Merge branch 'master' into master

This commit is contained in:
Carl Ouellette
2017-02-07 16:05:20 -05:00
committed by GitHub
9 changed files with 94 additions and 11 deletions

View File

@ -48,7 +48,7 @@ a WebSocket Server and Client for Arduino based on RFC6455.
This libary can run in Async TCP mode on the ESP. This libary can run in Async TCP mode on the ESP.
The mode can be aktivated in the ```WebSockets.h``` (see WEBSOCKETS_NETWORK_TYPE define). The mode can be activated in the ```WebSockets.h``` (see WEBSOCKETS_NETWORK_TYPE define).
[ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required. [ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required.

View File

@ -86,7 +86,7 @@ void setup() {
delay(100); delay(100);
} }
webSocket.begin("192.168.0.123", 81); webSocket.beginSocketIO("192.168.0.123", 81);
//webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization //webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization
webSocket.onEvent(webSocketEvent); webSocket.onEvent(webSocketEvent);

View File

@ -13,7 +13,7 @@
"type": "git", "type": "git",
"url": "https://github.com/Links2004/arduinoWebSockets.git" "url": "https://github.com/Links2004/arduinoWebSockets.git"
}, },
"version": "2.0.5", "version": "2.0.6",
"license": "LGPL-2.1", "license": "LGPL-2.1",
"export": { "export": {
"exclude": [ "exclude": [

View File

@ -1,5 +1,5 @@
name=WebSockets name=WebSockets
version=2.0.5 version=2.0.6
author=Markus Sattler author=Markus Sattler
maintainer=Markus Sattler maintainer=Markus Sattler
sentence=WebSockets for Arduino (Server + Client) sentence=WebSockets for Arduino (Server + Client)

View File

@ -430,7 +430,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
break; break;
case WSop_ping: case WSop_ping:
// send pong back // send pong back
sendFrame(client, WSop_pong, payload, header->payloadLen); sendFrame(client, WSop_pong, payload, header->payloadLen, true);
break; break;
case WSop_pong: case WSop_pong:
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get pong (%s)\n", client->num, payload); DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get pong (%s)\n", client->num, payload);

View File

@ -63,6 +63,7 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url,
_client.cVersion = 0; _client.cVersion = 0;
_client.base64Authorization = ""; _client.base64Authorization = "";
_client.plainAuthorization = ""; _client.plainAuthorization = "";
_client.isSocketIO = false;
#ifdef ESP8266 #ifdef ESP8266
randomSeed(RANDOM_REG32); randomSeed(RANDOM_REG32);
@ -207,6 +208,24 @@ bool WebSocketsClient::sendBIN(const uint8_t * payload, size_t length) {
return sendBIN((uint8_t *) payload, length); return sendBIN((uint8_t *) payload, length);
} }
/**
* sends a WS ping to Server
* @param payload uint8_t *
* @param length size_t
* @return true if ping is send out
*/
bool WebSocketsClient::sendPing(uint8_t * payload, size_t length) {
if(clientIsConnected(&_client)) {
return sendFrame(&_client, WSop_ping, payload, length, true);
}
return false;
}
bool WebSocketsClient::sendPing(String & payload) {
return sendPing((uint8_t *) payload.c_str(), payload.length());
}
/** /**
* disconnect one client * disconnect one client
* @param num uint8_t client id * @param num uint8_t client id
@ -313,6 +332,7 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {
client->cVersion = 0; client->cVersion = 0;
client->cIsUpgrade = false; client->cIsUpgrade = false;
client->cIsWebsocket = false; client->cIsWebsocket = false;
client->cSessionId = "";
client->status = WSC_NOT_CONNECTED; client->status = WSC_NOT_CONNECTED;
@ -411,8 +431,6 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
"Host: " + _host + ":" + _port + "\r\n" "Host: " + _host + ":" + _port + "\r\n"
"Connection: Upgrade\r\n" "Connection: Upgrade\r\n"
"Upgrade: websocket\r\n" "Upgrade: websocket\r\n"
"Origin: file://\r\n"
"User-Agent: arduino-WebSocket-Client\r\n"
"Sec-WebSocket-Version: 13\r\n" "Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: " + client->cKey + "\r\n"; "Sec-WebSocket-Key: " + client->cKey + "\r\n";
@ -426,11 +444,11 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
} else { } else {
handshake = "GET " + client->cUrl + "&transport=polling HTTP/1.1\r\n" handshake = "GET " + client->cUrl + "&transport=polling HTTP/1.1\r\n"
"Host: " + _host + ":" + _port + "\r\n"
"Connection: keep-alive\r\n"; "Connection: keep-alive\r\n";
} }
handshake += "Host: " + _host + ":" + _port + "\r\n" handshake += "Origin: file://\r\n"
"Origin: file://\r\n"
"User-Agent: arduino-WebSocket-Client\r\n"; "User-Agent: arduino-WebSocket-Client\r\n";
if(client->base64Authorization.length() > 0) { if(client->base64Authorization.length() > 0) {
@ -443,6 +461,7 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
handshake += "\r\n"; handshake += "\r\n";
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t*)handshake.c_str());
client->tcp->write((uint8_t*)handshake.c_str(), handshake.length()); client->tcp->write((uint8_t*)handshake.c_str(), handshake.length());
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
@ -489,7 +508,11 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Version")) { } else if(headerName.equalsIgnoreCase("Sec-WebSocket-Version")) {
client->cVersion = headerValue.toInt(); client->cVersion = headerValue.toInt();
} else if(headerName.equalsIgnoreCase("Set-Cookie")) { } else if(headerName.equalsIgnoreCase("Set-Cookie")) {
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1); if (headerValue.indexOf("HttpOnly") > -1) {
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1, headerValue.indexOf(";"));
} else {
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1);
}
} }
} else { } else {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header error (%s)\n", headerLine->c_str()); DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header error (%s)\n", headerLine->c_str());

View File

@ -68,6 +68,9 @@ class WebSocketsClient: private WebSockets {
bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false); bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
bool sendBIN(const uint8_t * payload, size_t length); bool sendBIN(const uint8_t * payload, size_t length);
bool sendPing(uint8_t * payload = NULL, size_t length = 0);
bool sendPing(String & payload);
void disconnect(void); void disconnect(void);
void setAuthorization(const char * user, const char * password); void setAuthorization(const char * user, const char * password);

View File

@ -283,6 +283,57 @@ bool WebSocketsServer::broadcastBIN(const uint8_t * payload, size_t length) {
return broadcastBIN((uint8_t *) payload, length); return broadcastBIN((uint8_t *) payload, length);
} }
/**
* sends a WS ping to Client
* @param num uint8_t client id
* @param payload uint8_t *
* @param length size_t
* @return true if ping is send out
*/
bool WebSocketsServer::sendPing(uint8_t num, uint8_t * payload, size_t length) {
if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
return false;
}
WSclient_t * client = &_clients[num];
if(clientIsConnected(client)) {
return sendFrame(client, WSop_ping, payload, length);
}
return false;
}
bool WebSocketsServer::sendPing(uint8_t num, String & payload) {
return sendPing(num, (uint8_t *) payload.c_str(), payload.length());
}
/**
* sends a WS ping to all Client
* @param payload uint8_t *
* @param length size_t
* @return true if ping is send out
*/
bool WebSocketsServer::broadcastPing(uint8_t * payload, size_t length) {
WSclient_t * client;
bool ret = true;
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
client = &_clients[i];
if(clientIsConnected(client)) {
if(!sendFrame(client, WSop_ping, payload, length)) {
ret = false;
}
}
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
delay(0);
#endif
}
return ret;
}
bool WebSocketsServer::broadcastPing(String & payload) {
return broadcastPing((uint8_t *) payload.c_str(), payload.length());
}
/** /**
* disconnect all clients * disconnect all clients
*/ */

View File

@ -32,7 +32,7 @@
class WebSocketsServer: private WebSockets { class WebSocketsServer: protected WebSockets {
public: public:
#ifdef __AVR__ #ifdef __AVR__
@ -80,6 +80,12 @@ public:
bool broadcastBIN(uint8_t * payload, size_t length, bool headerToPayload = false); bool broadcastBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
bool broadcastBIN(const uint8_t * payload, size_t length); bool broadcastBIN(const uint8_t * payload, size_t length);
bool sendPing(uint8_t num, uint8_t * payload = NULL, size_t length = 0);
bool sendPing(uint8_t num, String & payload);
bool broadcastPing(uint8_t * payload = NULL, size_t length = 0);
bool broadcastPing(String & payload);
void disconnect(void); void disconnect(void);
void disconnect(uint8_t num); void disconnect(uint8_t num);