Merge pull request #442 from Links2004/gpn19

Gpn19
This commit is contained in:
Markus
2019-06-20 10:41:29 +02:00
committed by GitHub
11 changed files with 813 additions and 724 deletions

63
.clang-format Normal file
View File

@ -0,0 +1,63 @@
---
BasedOnStyle: Google
AccessModifierOffset: '-2'
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlines: Left
AlignTrailingComments: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: 'true'
AllowShortLoopsOnASingleLine: 'true'
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: 'true'
AlwaysBreakTemplateDeclarations: 'false'
BinPackParameters: 'true'
BreakAfterJavaFieldAnnotations: 'false'
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: 'false'
BreakBeforeTernaryOperators: 'false'
BreakConstructorInitializers: BeforeColon
BreakStringLiterals: 'false'
ColumnLimit: '0'
CompactNamespaces: 'true'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '4'
Cpp11BracedListStyle: 'false'
DerivePointerAlignment: 'false'
FixNamespaceComments: 'true'
IndentCaseLabels: 'true'
IndentWidth: '4'
IndentWrappedFunctionNames: 'false'
JavaScriptQuotes: Single
JavaScriptWrapImports: 'false'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: All
ObjCBlockIndentWidth: '4'
ObjCSpaceAfterProperty: 'false'
ObjCSpaceBeforeProtocolList: 'false'
PointerAlignment: Middle
SortIncludes: 'false'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: Never
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '4'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
TabWidth: '4'
UseTab: Never
...

8
.gitignore vendored
View File

@ -27,3 +27,11 @@
*.out *.out
*.app *.app
/tests/webSocketServer/node_modules /tests/webSocketServer/node_modules
# IDE
.vscode
.cproject
.project
.settings
*.swp

View File

@ -45,6 +45,14 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
// send data to server // send data to server
// webSocket.sendBIN(payload, length); // webSocket.sendBIN(payload, length);
break; break;
case WStype_PING:
// pong will be send automatically
USE_SERIAL.printf("[WSc] get ping\n");
break;
case WStype_PONG:
// answer to a ping we send
USE_SERIAL.printf("[WSc] get pong\n");
break;
} }
} }

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.1.3", "version": "2.1.4",
"license": "LGPL-2.1", "license": "LGPL-2.1",
"export": { "export": {
"exclude": [ "exclude": [

View File

@ -1,5 +1,5 @@
name=WebSockets name=WebSockets
version=2.1.3 version=2.1.4
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

@ -48,7 +48,6 @@ extern "C" {
#endif #endif
/** /**
* *
* @param client WSclient_t * ptr to the client struct * @param client WSclient_t * ptr to the client struct
@ -82,7 +81,6 @@ void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * rea
* @return true if ok * @return true if ok
*/ */
bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin, bool headerToPayload) { bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin, bool headerToPayload) {
if(client->tcp && !client->tcp->connected()) { if(client->tcp && !client->tcp->connected()) {
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] not Connected!?\n", client->num); DEBUG_WEBSOCKETS("[WS][%d][sendFrame] not Connected!?\n", client->num);
return false; return false;
@ -318,12 +316,12 @@ bool WebSockets::handleWebsocketWaitFor(WSclient_t * client, size_t size) {
// timeout or error // timeout or error
server->clientDisconnect(client, 1002); server->clientDisconnect(client, 1002);
} }
}, this, size, std::placeholders::_1, std::placeholders::_2)); },
this, size, std::placeholders::_1, std::placeholders::_2));
return false; return false;
} }
void WebSockets::handleWebsocketCb(WSclient_t * client) { void WebSockets::handleWebsocketCb(WSclient_t * client) {
if(!client->tcp || !client->tcp->connected()) { if(!client->tcp || !client->tcp->connected()) {
return; return;
} }
@ -409,7 +407,6 @@ void WebSockets::handleWebsocketCb(WSclient_t * client) {
} }
void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t * payload) { void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t * payload) {
WSMessageHeader_t * header = &client->cWsHeaderDecode; WSMessageHeader_t * header = &client->cWsHeaderDecode;
if(ok) { if(ok) {
if(header->payloadLen > 0) { if(header->payloadLen > 0) {
@ -435,10 +432,12 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
// send pong back // send pong back
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] ping received (%s)\n", client->num, payload ? (const char *)payload : ""); DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] ping received (%s)\n", client->num, payload ? (const char *)payload : "");
sendFrame(client, WSop_pong, payload, header->payloadLen); sendFrame(client, WSop_pong, payload, header->payloadLen);
messageReceived(client, header->opCode, payload, header->payloadLen, header->fin);
break; break;
case WSop_pong: case WSop_pong:
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get pong (%s)\n", client->num, payload ? (const char *)payload : ""); DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get pong (%s)\n", client->num, payload ? (const char *)payload : "");
client->pongReceived = true; client->pongReceived = true;
messageReceived(client, header->opCode, payload, header->payloadLen, header->fin);
break; break;
case WSop_close: { case WSop_close: {
#ifndef NODEBUG_WEBSOCKETS #ifndef NODEBUG_WEBSOCKETS
@ -454,8 +453,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
DEBUG_WEBSOCKETS("\n"); DEBUG_WEBSOCKETS("\n");
} }
clientDisconnect(client, 1000); clientDisconnect(client, 1000);
} } break;
break;
default: default:
clientDisconnect(client, 1002); clientDisconnect(client, 1002);
break; break;
@ -544,7 +542,8 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
if(cb) { if(cb) {
cb(client, ok); cb(client, ok);
} }
}, client, std::placeholders::_1, cb)); },
client, std::placeholders::_1, cb));
#else #else
unsigned long t = millis(); unsigned long t = millis();
@ -610,8 +609,10 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
* @return bytes send * @return bytes send
*/ */
size_t WebSockets::write(WSclient_t * client, uint8_t * out, size_t n) { size_t WebSockets::write(WSclient_t * client, uint8_t * out, size_t n) {
if(out == NULL) return 0; if(out == NULL)
if(client == NULL) return 0; return 0;
if(client == NULL)
return 0;
unsigned long t = millis(); unsigned long t = millis();
size_t len = 0; size_t len = 0;
size_t total = 0; size_t total = 0;
@ -650,8 +651,10 @@ size_t WebSockets::write(WSclient_t * client, uint8_t *out, size_t n) {
} }
size_t WebSockets::write(WSclient_t * client, const char * out) { size_t WebSockets::write(WSclient_t * client, const char * out) {
if(client == NULL) return 0; if(client == NULL)
if(out == NULL) return 0; return 0;
if(out == NULL)
return 0;
return write(client, (uint8_t *)out, strlen(out)); return write(client, (uint8_t *)out, strlen(out));
} }
@ -663,11 +666,12 @@ size_t WebSockets::write(WSclient_t * client, const char *out) {
* @param disconnectTimeoutCount uint8_t how many timeouts before disconnect, 0=> do not disconnect * @param disconnectTimeoutCount uint8_t how many timeouts before disconnect, 0=> do not disconnect
*/ */
void WebSockets::enableHeartbeat(WSclient_t * client, uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount) { void WebSockets::enableHeartbeat(WSclient_t * client, uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount) {
if(client == NULL) return; if(client == NULL)
return;
client->pingInterval = pingInterval; client->pingInterval = pingInterval;
client->pongTimeout = pongTimeout; client->pongTimeout = pongTimeout;
client->disconnectTimeoutCount = disconnectTimeoutCount; client->disconnectTimeoutCount = disconnectTimeoutCount;
client->pongReceived = false;
} }
/** /**
@ -693,7 +697,5 @@ void WebSockets::handleHBTimeout(WSclient_t * client){
} }
} }
} }
} }
} }

View File

@ -40,7 +40,6 @@
#include <functional> #include <functional>
#endif #endif
#ifndef NODEBUG_WEBSOCKETS #ifndef NODEBUG_WEBSOCKETS
#ifdef DEBUG_ESP_PORT #ifdef DEBUG_ESP_PORT
#define DEBUG_WEBSOCKETS(...) DEBUG_ESP_PORT.printf(__VA_ARGS__) #define DEBUG_WEBSOCKETS(...) DEBUG_ESP_PORT.printf(__VA_ARGS__)
@ -49,7 +48,6 @@
#endif #endif
#endif #endif
#ifndef DEBUG_WEBSOCKETS #ifndef DEBUG_WEBSOCKETS
#define DEBUG_WEBSOCKETS(...) #define DEBUG_WEBSOCKETS(...)
#define NODEBUG_WEBSOCKETS #define NODEBUG_WEBSOCKETS
@ -78,7 +76,6 @@
#endif #endif
#define WEBSOCKETS_TCP_TIMEOUT (2000) #define WEBSOCKETS_TCP_TIMEOUT (2000)
#define NETWORK_ESP8266_ASYNC (0) #define NETWORK_ESP8266_ASYNC (0)
@ -86,6 +83,7 @@
#define NETWORK_W5100 (2) #define NETWORK_W5100 (2)
#define NETWORK_ENC28J60 (3) #define NETWORK_ENC28J60 (3)
#define NETWORK_ESP32 (4) #define NETWORK_ESP32 (4)
#define NETWORK_ESP32_ETH (5)
// max size of the WS Message Header // max size of the WS Message Header
#define WEBSOCKETS_MAX_HEADER_SIZE (14) #define WEBSOCKETS_MAX_HEADER_SIZE (14)
@ -99,7 +97,7 @@
#elif defined(ESP32) #elif defined(ESP32)
#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP32 #define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP32
//#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP32_ETH
#else #else
#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100 #define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
@ -113,7 +111,6 @@
// No SSL/WSS support for client in Async mode // No SSL/WSS support for client in Async mode
// TLS lib need a sync interface! // TLS lib need a sync interface!
#if defined(ESP8266) #if defined(ESP8266)
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#elif defined(ESP32) #elif defined(ESP32)
@ -142,6 +139,7 @@
#include <ESP31BWiFi.h> #include <ESP31BWiFi.h>
#endif #endif
#define WEBSOCKETS_NETWORK_CLASS WiFiClient #define WEBSOCKETS_NETWORK_CLASS WiFiClient
#define WEBSOCKETS_NETWORK_SSL_CLASS WiFiClientSecure
#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer #define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
#elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100) #elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100)
@ -167,12 +165,23 @@
#include <WiFi.h> #include <WiFi.h>
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
#define WEBSOCKETS_NETWORK_CLASS WiFiClient #define WEBSOCKETS_NETWORK_CLASS WiFiClient
#define WEBSOCKETS_NETWORK_SSL_CLASS WiFiClientSecure
#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
#elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32_ETH)
#include <ETH.h>
#define WEBSOCKETS_NETWORK_CLASS WiFiClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer #define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
#else #else
#error "no network type selected!" #error "no network type selected!"
#endif #endif
#ifdef WEBSOCKETS_NETWORK_SSL_CLASS
#define HAS_SSL
#endif
// moves all Header strings to Flash (~300 Byte) // moves all Header strings to Flash (~300 Byte)
#ifdef WEBSOCKETS_SAVE_RAM #ifdef WEBSOCKETS_SAVE_RAM
#define WEBSOCKETS_STRING(var) F(var) #define WEBSOCKETS_STRING(var) F(var)
@ -196,6 +205,8 @@ typedef enum {
WStype_FRAGMENT_BIN_START, WStype_FRAGMENT_BIN_START,
WStype_FRAGMENT, WStype_FRAGMENT,
WStype_FRAGMENT_FIN, WStype_FRAGMENT_FIN,
WStype_PING,
WStype_PONG,
} WStype_t; } WStype_t;
typedef enum { typedef enum {
@ -210,7 +221,6 @@ typedef enum {
} WSopcode_t; } WSopcode_t;
typedef struct { typedef struct {
bool fin; bool fin;
bool rsv1; bool rsv1;
bool rsv2; bool rsv2;
@ -233,9 +243,9 @@ typedef struct {
bool isSocketIO; ///< client for socket.io server bool isSocketIO; ///< client for socket.io server
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
bool isSSL; ///< run in ssl mode bool isSSL; ///< run in ssl mode
WiFiClientSecure * ssl; WEBSOCKETS_NETWORK_SSL_CLASS * ssl;
#endif #endif
String cUrl; ///< http url String cUrl; ///< http url
@ -277,8 +287,6 @@ typedef struct {
} WSclient_t; } WSclient_t;
class WebSockets { class WebSockets {
protected: protected:
#ifdef __AVR__ #ifdef __AVR__
@ -312,8 +320,6 @@ class WebSockets {
void enableHeartbeat(WSclient_t * client, uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount); void enableHeartbeat(WSclient_t * client, uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
void handleHBTimeout(WSclient_t * client); void handleHBTimeout(WSclient_t * client);
}; };
#ifndef UNUSED #ifndef UNUSED

View File

@ -42,14 +42,15 @@ WebSocketsClient::~WebSocketsClient() {
void WebSocketsClient::begin(const char * host, uint16_t port, const char * url, const char * protocol) { void WebSocketsClient::begin(const char * host, uint16_t port, const char * url, const char * protocol) {
_host = host; _host = host;
_port = port; _port = port;
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
_fingerprint = ""; _fingerprint = "";
_CA_cert = NULL;
#endif #endif
_client.num = 0; _client.num = 0;
_client.status = WSC_NOT_CONNECTED; _client.status = WSC_NOT_CONNECTED;
_client.tcp = NULL; _client.tcp = NULL;
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
_client.isSSL = false; _client.isSSL = false;
_client.ssl = NULL; _client.ssl = NULL;
#endif #endif
@ -92,16 +93,24 @@ void WebSocketsClient::begin(IPAddress host, uint16_t port, const char * url, co
return begin(host.toString().c_str(), port, url, protocol); return begin(host.toString().c_str(), port, url, protocol);
} }
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
void WebSocketsClient::beginSSL(const char * host, uint16_t port, const char * url, const char * fingerprint, const char * protocol) { void WebSocketsClient::beginSSL(const char * host, uint16_t port, const char * url, const char * fingerprint, const char * protocol) {
begin(host, port, url, protocol); begin(host, port, url, protocol);
_client.isSSL = true; _client.isSSL = true;
_fingerprint = fingerprint; _fingerprint = fingerprint;
_CA_cert = NULL;
} }
void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String fingerprint, String protocol) { void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String fingerprint, String protocol) {
beginSSL(host.c_str(), port, url.c_str(), fingerprint.c_str(), protocol.c_str()); beginSSL(host.c_str(), port, url.c_str(), fingerprint.c_str(), protocol.c_str());
} }
void WebSocketsClient::beginSslWithCA(const char * host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
begin(host, port, url, protocol);
_client.isSSL = true;
_fingerprint = "";
_CA_cert = CA_cert;
}
#endif #endif
void WebSocketsClient::beginSocketIO(const char * host, uint16_t port, const char * url, const char * protocol) { void WebSocketsClient::beginSocketIO(const char * host, uint16_t port, const char * url, const char * protocol) {
@ -113,7 +122,7 @@ void WebSocketsClient::beginSocketIO(String host, uint16_t port, String url, Str
beginSocketIO(host.c_str(), port, url.c_str(), protocol.c_str()); beginSocketIO(host.c_str(), port, url.c_str(), protocol.c_str());
} }
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
void WebSocketsClient::beginSocketIOSSL(const char * host, uint16_t port, const char * url, const char * protocol) { void WebSocketsClient::beginSocketIOSSL(const char * host, uint16_t port, const char * url, const char * protocol) {
begin(host, port, url, protocol); begin(host, port, url, protocol);
_client.isSocketIO = true; _client.isSocketIO = true;
@ -124,6 +133,14 @@ void WebSocketsClient::beginSocketIOSSL(const char *host, uint16_t port, const c
void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url, String protocol) { void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url, String protocol) {
beginSocketIOSSL(host.c_str(), port, url.c_str(), protocol.c_str()); beginSocketIOSSL(host.c_str(), port, url.c_str(), protocol.c_str());
} }
void WebSocketsClient::beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
begin(host, port, url, protocol);
_client.isSocketIO = true;
_client.isSSL = true;
_fingerprint = "";
_CA_cert = CA_cert;
}
#endif #endif
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
@ -137,7 +154,7 @@ void WebSocketsClient::loop(void) {
return; return;
} }
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
if(_client.isSSL) { if(_client.isSSL) {
DEBUG_WEBSOCKETS("[WS-Client] connect wss...\n"); DEBUG_WEBSOCKETS("[WS-Client] connect wss...\n");
if(_client.ssl) { if(_client.ssl) {
@ -145,15 +162,25 @@ void WebSocketsClient::loop(void) {
_client.ssl = NULL; _client.ssl = NULL;
_client.tcp = NULL; _client.tcp = NULL;
} }
_client.ssl = new WiFiClientSecure(); _client.ssl = new WEBSOCKETS_NETWORK_SSL_CLASS();
_client.tcp = _client.ssl; _client.tcp = _client.ssl;
if(_CA_cert) {
DEBUG_WEBSOCKETS("[WS-Client] setting CA certificate");
#if defined(ESP32)
_client.ssl->setCACert(_CA_cert);
#elif defined(ESP8266)
_client.ssl->setCACert((const uint8_t *)_CA_cert, strlen(_CA_cert) + 1);
#else
#error setCACert not implemented
#endif
}
} else { } else {
DEBUG_WEBSOCKETS("[WS-Client] connect ws...\n"); DEBUG_WEBSOCKETS("[WS-Client] connect ws...\n");
if(_client.tcp) { if(_client.tcp) {
delete _client.tcp; delete _client.tcp;
_client.tcp = NULL; _client.tcp = NULL;
} }
_client.tcp = new WiFiClient(); _client.tcp = new WEBSOCKETS_NETWORK_CLASS();
} }
#else #else
_client.tcp = new WEBSOCKETS_NETWORK_CLASS(); _client.tcp = new WEBSOCKETS_NETWORK_CLASS();
@ -163,14 +190,16 @@ void WebSocketsClient::loop(void) {
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!"); DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!");
return; return;
} }
#if defined(ESP32)
if(_client.tcp->connect(_host.c_str(), _port, WEBSOCKETS_TCP_TIMEOUT)) {
#else
if(_client.tcp->connect(_host.c_str(), _port)) { if(_client.tcp->connect(_host.c_str(), _port)) {
#endif
connectedCb(); connectedCb();
_lastConnectionFail = 0; _lastConnectionFail = 0;
} else { } else {
connectFailedCb(); connectFailedCb();
_lastConnectionFail = millis(); _lastConnectionFail = millis();
} }
} else { } else {
handleClientData(); handleClientData();
@ -179,7 +208,6 @@ void WebSocketsClient::loop(void) {
handleHBPing(); handleHBPing();
handleHBTimeout(&_client); handleHBTimeout(&_client);
} }
} }
} }
#endif #endif
@ -344,15 +372,18 @@ void WebSocketsClient::messageReceived(WSclient_t * client, WSopcode_t opcode, u
case WSop_continuation: case WSop_continuation:
type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT; type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT;
break; break;
case WSop_close:
case WSop_ping: case WSop_ping:
type = WStype_PING;
break;
case WSop_pong: case WSop_pong:
type = WStype_PONG;
break;
case WSop_close:
default: default:
break; break;
} }
runCbEvent(type, payload, length); runCbEvent(type, payload, length);
} }
/** /**
@ -360,7 +391,6 @@ void WebSocketsClient::messageReceived(WSclient_t * client, WSopcode_t opcode, u
* @param client WSclient_t * ptr to the client struct * @param client WSclient_t * ptr to the client struct
*/ */
void WebSocketsClient::clientDisconnect(WSclient_t * client) { void WebSocketsClient::clientDisconnect(WSclient_t * client) {
bool event = false; bool event = false;
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
@ -414,7 +444,6 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {
* @return true = conneted * @return true = conneted
*/ */
bool WebSocketsClient::clientIsConnected(WSclient_t * client) { bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
if(!client->tcp) { if(!client->tcp) {
return false; return false;
} }
@ -450,8 +479,7 @@ void WebSocketsClient::handleClientData(void) {
case WSC_HEADER: { case WSC_HEADER: {
String headerLine = _client.tcp->readStringUntil('\n'); String headerLine = _client.tcp->readStringUntil('\n');
handleHeader(&_client, &headerLine); handleHeader(&_client, &headerLine);
} } break;
break;
case WSC_CONNECTED: case WSC_CONNECTED:
WebSockets::handleWebsocket(&_client); WebSockets::handleWebsocket(&_client);
break; break;
@ -471,7 +499,6 @@ void WebSocketsClient::handleClientData(void) {
* @param client WSclient_t * ptr to the client struct * @param client WSclient_t * ptr to the client struct
*/ */
void WebSocketsClient::sendHeader(WSclient_t * client) { void WebSocketsClient::sendHeader(WSclient_t * client) {
static const char * NEW_LINE = "\r\n"; static const char * NEW_LINE = "\r\n";
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header...\n"); DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header...\n");
@ -503,12 +530,14 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
} }
handshake = WEBSOCKETS_STRING("GET "); handshake = WEBSOCKETS_STRING("GET ");
handshake += url + WEBSOCKETS_STRING(" HTTP/1.1\r\n" handshake += url + WEBSOCKETS_STRING(
" HTTP/1.1\r\n"
"Host: "); "Host: ");
handshake += _host + ":" + _port + NEW_LINE; handshake += _host + ":" + _port + NEW_LINE;
if(ws_header) { if(ws_header) {
handshake += WEBSOCKETS_STRING("Connection: Upgrade\r\n" handshake += WEBSOCKETS_STRING(
"Connection: Upgrade\r\n"
"Upgrade: websocket\r\n" "Upgrade: websocket\r\n"
"Sec-WebSocket-Version: 13\r\n" "Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: "); "Sec-WebSocket-Key: ");
@ -554,7 +583,6 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
#endif #endif
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header... Done (%luus).\n", (micros() - start)); DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header... Done (%luus).\n", (micros() - start));
} }
/** /**
@ -562,7 +590,6 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
* @param client WSclient_t * ptr to the client struct * @param client WSclient_t * ptr to the client struct
*/ */
void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) { void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
headerLine->trim(); // remove \r headerLine->trim(); // remove \r
if(headerLine->length() > 0) { if(headerLine->length() > 0) {
@ -571,7 +598,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
if(headerLine->startsWith(WEBSOCKETS_STRING("HTTP/1."))) { if(headerLine->startsWith(WEBSOCKETS_STRING("HTTP/1."))) {
// "HTTP/1.1 101 Switching Protocols" // "HTTP/1.1 101 Switching Protocols"
client->cCode = headerLine->substring(9, headerLine->indexOf(' ', 9)).toInt(); client->cCode = headerLine->substring(9, headerLine->indexOf(' ', 9)).toInt();
} else if(headerLine->indexOf(':')) { } else if(headerLine->indexOf(':') >= 0) {
String headerName = headerLine->substring(0, headerLine->indexOf(':')); String headerName = headerLine->substring(0, headerLine->indexOf(':'));
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1); String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
@ -612,7 +639,6 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine))); client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
#endif #endif
} else { } else {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header read fin.\n"); DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header read fin.\n");
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Client settings:\n"); DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Client settings:\n");
@ -653,7 +679,6 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
} }
if(ok) { if(ok) {
if(client->cAccept.length() == 0) { if(client->cAccept.length() == 0) {
ok = false; ok = false;
} else { } else {
@ -667,12 +692,10 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
} }
if(ok) { if(ok) {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Websocket connection init done.\n"); DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Websocket connection init done.\n");
headerDone(client); headerDone(client);
runCbEvent(WStype_CONNECTED, (uint8_t *)client->cUrl.c_str(), client->cUrl.length()); runCbEvent(WStype_CONNECTED, (uint8_t *)client->cUrl.c_str(), client->cUrl.length());
} else if(clientIsConnected(client) && client->isSocketIO && client->cSessionId.length() > 0) { } else if(clientIsConnected(client) && client->isSocketIO && client->cSessionId.length() > 0) {
sendHeader(client); sendHeader(client);
} else { } else {
@ -687,7 +710,6 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
} }
void WebSocketsClient::connectedCb() { void WebSocketsClient::connectedCb() {
DEBUG_WEBSOCKETS("[WS-Client] connected to %s:%u.\n", _host.c_str(), _port); DEBUG_WEBSOCKETS("[WS-Client] connected to %s:%u.\n", _host.c_str(), _port);
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
@ -700,7 +722,8 @@ void WebSocketsClient::connectedCb() {
c->asyncConnect(); c->asyncConnect();
return true; return true;
}, this, std::placeholders::_1, &_client)); },
this, std::placeholders::_1, &_client));
#endif #endif
_client.status = WSC_HEADER; _client.status = WSC_HEADER;
@ -710,21 +733,26 @@ void WebSocketsClient::connectedCb() {
_client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT); _client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
#endif #endif
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
_client.tcp->setNoDelay(true); _client.tcp->setNoDelay(true);
#endif
#if defined(HAS_SSL)
if(_client.isSSL && _fingerprint.length()) { if(_client.isSSL && _fingerprint.length()) {
if(!_client.ssl->verify(_fingerprint.c_str(), _host.c_str())) { if(!_client.ssl->verify(_fingerprint.c_str(), _host.c_str())) {
DEBUG_WEBSOCKETS("[WS-Client] certificate mismatch\n"); DEBUG_WEBSOCKETS("[WS-Client] certificate mismatch\n");
WebSockets::clientDisconnect(&_client, 1000); WebSockets::clientDisconnect(&_client, 1000);
return; return;
} }
} else if(_client.isSSL && !_CA_cert) {
#if defined(wificlientbearssl_h) && !defined(USING_AXTLS)
_client.ssl->setInsecure();
#endif
} }
#endif #endif
// send Header to Server // send Header to Server
sendHeader(&_client); sendHeader(&_client);
} }
void WebSocketsClient::connectFailedCb() { void WebSocketsClient::connectFailedCb() {
@ -734,7 +762,6 @@ void WebSocketsClient::connectFailedCb() {
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
void WebSocketsClient::asyncConnect() { void WebSocketsClient::asyncConnect() {
DEBUG_WEBSOCKETS("[WS-Client] asyncConnect...\n"); DEBUG_WEBSOCKETS("[WS-Client] asyncConnect...\n");
AsyncClient * tcpclient = new AsyncClient(); AsyncClient * tcpclient = new AsyncClient();
@ -757,20 +784,21 @@ void WebSocketsClient::asyncConnect() {
return; return;
} }
ws->connectedCb(); ws->connectedCb();
}, this, std::placeholders::_2)); },
this, std::placeholders::_2));
tcpclient->onError(std::bind([](WebSocketsClient * ws, AsyncClient * tcp) { tcpclient->onError(std::bind([](WebSocketsClient * ws, AsyncClient * tcp) {
ws->connectFailedCb(); ws->connectFailedCb();
// reconnect // reconnect
ws->asyncConnect(); ws->asyncConnect();
}, this, std::placeholders::_2)); },
this, std::placeholders::_2));
if(!tcpclient->connect(_host.c_str(), _port)) { if(!tcpclient->connect(_host.c_str(), _port)) {
connectFailedCb(); connectFailedCb();
delete tcpclient; delete tcpclient;
} }
} }
#endif #endif
@ -779,7 +807,8 @@ void WebSocketsClient::asyncConnect() {
* send heartbeat ping to server in set intervals * send heartbeat ping to server in set intervals
*/ */
void WebSocketsClient::handleHBPing() { void WebSocketsClient::handleHBPing() {
if (_client.pingInterval == 0) return; if(_client.pingInterval == 0)
return;
uint32_t pi = millis() - _client.lastPing; uint32_t pi = millis() - _client.lastPing;
if(pi > _client.pingInterval) { if(pi > _client.pingInterval) {
DEBUG_WEBSOCKETS("[WS-Client] sending HB ping\n"); DEBUG_WEBSOCKETS("[WS-Client] sending HB ping\n");
@ -788,7 +817,6 @@ void WebSocketsClient::handleHBPing(){
_client.pongReceived = false; _client.pongReceived = false;
} }
} }
} }
/** /**

View File

@ -35,7 +35,6 @@ class WebSocketsClient: private WebSockets {
typedef std::function<void(WStype_t type, uint8_t * payload, size_t length)> WebSocketClientEvent; typedef std::function<void(WStype_t type, uint8_t * payload, size_t length)> WebSocketClientEvent;
#endif #endif
WebSocketsClient(void); WebSocketsClient(void);
virtual ~WebSocketsClient(void); virtual ~WebSocketsClient(void);
@ -43,17 +42,19 @@ class WebSocketsClient: private WebSockets {
void begin(String host, uint16_t port, String url = "/", String protocol = "arduino"); void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");
void begin(IPAddress host, uint16_t port, const char * url = "/", const char * protocol = "arduino"); void begin(IPAddress host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
void beginSSL(const char * host, uint16_t port, const char * url = "/", const char * = "", const char * protocol = "arduino"); void beginSSL(const char * host, uint16_t port, const char * url = "/", const char * = "", const char * protocol = "arduino");
void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "", String protocol = "arduino"); void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "", String protocol = "arduino");
void beginSslWithCA(const char * host, uint16_t port, const char * url = "/", const char * CA_cert = NULL, const char * protocol = "arduino");
#endif #endif
void beginSocketIO(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino"); void beginSocketIO(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino"); void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
void beginSocketIOSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino"); void beginSocketIOSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino"); void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
void beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
#endif #endif
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
@ -93,8 +94,9 @@ class WebSocketsClient: private WebSockets {
String _host; String _host;
uint16_t _port; uint16_t _port;
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if defined(HAS_SSL)
String _fingerprint; String _fingerprint;
const char * _CA_cert;
#endif #endif
WSclient_t _client; WSclient_t _client;
@ -135,7 +137,6 @@ class WebSocketsClient: private WebSockets {
_cbEvent(type, payload, length); _cbEvent(type, payload, length);
} }
} }
}; };
#endif /* WEBSOCKETSCLIENT_H_ */ #endif /* WEBSOCKETSCLIENT_H_ */

View File

@ -36,7 +36,8 @@ WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
_server->onClient([](void * s, AsyncClient * c) { _server->onClient([](void * s, AsyncClient * c) {
((WebSocketsServer *)s)->newClient(new AsyncTCPbuffer(c)); ((WebSocketsServer *)s)->newClient(new AsyncTCPbuffer(c));
}, this); },
this);
#endif #endif
_cbEvent = NULL; _cbEvent = NULL;
@ -48,7 +49,6 @@ WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol
memset(&_clients[0], 0x00, (sizeof(WSclient_t) * WEBSOCKETS_SERVER_CLIENT_MAX)); memset(&_clients[0], 0x00, (sizeof(WSclient_t) * WEBSOCKETS_SERVER_CLIENT_MAX));
} }
WebSocketsServer::~WebSocketsServer() { WebSocketsServer::~WebSocketsServer() {
// disconnect all clients // disconnect all clients
close(); close();
@ -120,7 +120,6 @@ void WebSocketsServer::close(void) {
#else #else
// TODO how to close server? // TODO how to close server?
#endif #endif
} }
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
@ -152,8 +151,7 @@ void WebSocketsServer::onEvent(WebSocketServerEvent cbEvent) {
void WebSocketsServer::onValidateHttpHeader( void WebSocketsServer::onValidateHttpHeader(
WebSocketServerHttpHeaderValFunc validationFunc, WebSocketServerHttpHeaderValFunc validationFunc,
const char * mandatoryHttpHeaders[], const char * mandatoryHttpHeaders[],
size_t mandatoryHttpHeaderCount) size_t mandatoryHttpHeaderCount) {
{
_httpHeaderValidationFunc = validationFunc; _httpHeaderValidationFunc = validationFunc;
if(_mandatoryHttpHeaders) if(_mandatoryHttpHeaders)
@ -300,7 +298,6 @@ 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 * sends a WS ping to Client
* @param num uint8_t client id * @param num uint8_t client id
@ -350,7 +347,6 @@ bool WebSocketsServer::broadcastPing(String & payload) {
return broadcastPing((uint8_t *)payload.c_str(), payload.length()); return broadcastPing((uint8_t *)payload.c_str(), payload.length());
} }
/** /**
* disconnect all clients * disconnect all clients
*/ */
@ -378,7 +374,6 @@ void WebSocketsServer::disconnect(uint8_t num) {
} }
} }
/* /*
* set the Authorization for the http request * set the Authorization for the http request
* @param user const char * * @param user const char *
@ -455,7 +450,6 @@ bool WebSocketsServer::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient) {
// state is not connected or tcp connection is lost // state is not connected or tcp connection is lost
if(!clientIsConnected(client)) { if(!clientIsConnected(client)) {
client->tcp = TCPclient; client->tcp = TCPclient;
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
@ -474,7 +468,6 @@ bool WebSocketsServer::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient) {
DEBUG_WEBSOCKETS("[WS-Server][%d] new client\n", client->num); DEBUG_WEBSOCKETS("[WS-Server][%d] new client\n", client->num);
#endif #endif
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
client->tcp->onDisconnect(std::bind([](WebSocketsServer * server, AsyncTCPbuffer * obj, WSclient_t * client) -> bool { client->tcp->onDisconnect(std::bind([](WebSocketsServer * server, AsyncTCPbuffer * obj, WSclient_t * client) -> bool {
DEBUG_WEBSOCKETS("[WS-Server][%d] Disconnect client\n", client->num); DEBUG_WEBSOCKETS("[WS-Server][%d] Disconnect client\n", client->num);
@ -485,8 +478,8 @@ bool WebSocketsServer::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient) {
*sl = NULL; *sl = NULL;
} }
return true; return true;
}, this, std::placeholders::_1, client)); },
this, std::placeholders::_1, client));
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsServer::handleHeader, this, client, &(client->cHttpLine))); client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsServer::handleHeader, this, client, &(client->cHttpLine)));
#endif #endif
@ -518,15 +511,18 @@ void WebSocketsServer::messageReceived(WSclient_t * client, WSopcode_t opcode, u
case WSop_continuation: case WSop_continuation:
type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT; type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT;
break; break;
case WSop_close:
case WSop_ping: case WSop_ping:
type = WStype_PING;
break;
case WSop_pong: case WSop_pong:
type = WStype_PONG;
break;
case WSop_close:
default: default:
break; break;
} }
runCbEvent(client->num, type, payload, length); runCbEvent(client->num, type, payload, length);
} }
/** /**
@ -534,8 +530,6 @@ void WebSocketsServer::messageReceived(WSclient_t * client, WSopcode_t opcode, u
* @param client WSclient_t * ptr to the client struct * @param client WSclient_t * ptr to the client struct
*/ */
void WebSocketsServer::clientDisconnect(WSclient_t * client) { void WebSocketsServer::clientDisconnect(WSclient_t * client) {
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
if(client->isSSL && client->ssl) { if(client->isSSL && client->ssl) {
if(client->ssl->connected()) { if(client->ssl->connected()) {
@ -581,7 +575,6 @@ void WebSocketsServer::clientDisconnect(WSclient_t * client) {
DEBUG_WEBSOCKETS("[WS-Server][%d] client disconnected.\n", client->num); DEBUG_WEBSOCKETS("[WS-Server][%d] client disconnected.\n", client->num);
runCbEvent(client->num, WStype_DISCONNECTED, NULL, 0); runCbEvent(client->num, WStype_DISCONNECTED, NULL, 0);
} }
/** /**
@ -590,7 +583,6 @@ void WebSocketsServer::clientDisconnect(WSclient_t * client) {
* @return true = connected * @return true = connected
*/ */
bool WebSocketsServer::clientIsConnected(WSclient_t * client) { bool WebSocketsServer::clientIsConnected(WSclient_t * client) {
if(!client->tcp) { if(!client->tcp) {
return false; return false;
} }
@ -621,7 +613,6 @@ bool WebSocketsServer::clientIsConnected(WSclient_t * client) {
* Handle incoming Connection Request * Handle incoming Connection Request
*/ */
void WebSocketsServer::handleNewClients(void) { void WebSocketsServer::handleNewClients(void) {
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
while(_server->hasClient()) { while(_server->hasClient()) {
#endif #endif
@ -656,15 +647,12 @@ void WebSocketsServer::handleNewClients(void) {
delay(0); delay(0);
} }
#endif #endif
} }
/** /**
* Handel incomming data from Client * Handel incomming data from Client
*/ */
void WebSocketsServer::handleClientData(void) { void WebSocketsServer::handleClientData(void) {
WSclient_t * client; WSclient_t * client;
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
client = &_clients[i]; client = &_clients[i];
@ -673,12 +661,10 @@ void WebSocketsServer::handleClientData(void) {
if(len > 0) { if(len > 0) {
//DEBUG_WEBSOCKETS("[WS-Server][%d][handleClientData] len: %d\n", client->num, len); //DEBUG_WEBSOCKETS("[WS-Server][%d][handleClientData] len: %d\n", client->num, len);
switch(client->status) { switch(client->status) {
case WSC_HEADER: case WSC_HEADER: {
{
String headerLine = client->tcp->readStringUntil('\n'); String headerLine = client->tcp->readStringUntil('\n');
handleHeader(client, &headerLine); handleHeader(client, &headerLine);
} } break;
break;
case WSC_CONNECTED: case WSC_CONNECTED:
WebSockets::handleWebsocket(client); WebSockets::handleWebsocket(client);
break; break;
@ -707,14 +693,12 @@ bool WebSocketsServer::hasMandatoryHeader(String headerName) {
return false; return false;
} }
/** /**
* handles http header reading for WebSocket upgrade * handles http header reading for WebSocket upgrade
* @param client WSclient_t * ///< pointer to the client struct * @param client WSclient_t * ///< pointer to the client struct
* @param headerLine String ///< the header being read / processed * @param headerLine String ///< the header being read / processed
*/ */
void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) { void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
static const char * NEW_LINE = "\r\n"; static const char * NEW_LINE = "\r\n";
headerLine->trim(); // remove \r headerLine->trim(); // remove \r
@ -724,7 +708,6 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
// websocket requests always start with GET see rfc6455 // websocket requests always start with GET see rfc6455
if(headerLine->startsWith("GET ")) { if(headerLine->startsWith("GET ")) {
// cut URL out // cut URL out
client->cUrl = headerLine->substring(4, headerLine->indexOf(' ', 4)); client->cUrl = headerLine->substring(4, headerLine->indexOf(' ', 4));
@ -732,7 +715,7 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
client->cHttpHeadersValid = true; client->cHttpHeadersValid = true;
client->cMandatoryHeadersCount = 0; client->cMandatoryHeadersCount = 0;
} else if(headerLine->indexOf(':')) { } else if(headerLine->indexOf(':') >= 0) {
String headerName = headerLine->substring(0, headerLine->indexOf(':')); String headerName = headerLine->substring(0, headerLine->indexOf(':'));
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1); String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
@ -777,7 +760,6 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsServer::handleHeader, this, client, &(client->cHttpLine))); client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsServer::handleHeader, this, client, &(client->cHttpLine)));
#endif #endif
} else { } else {
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] Header read fin.\n", client->num); DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] Header read fin.\n", client->num);
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cURL: %s\n", client->num, client->cUrl.c_str()); DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cURL: %s\n", client->num, client->cUrl.c_str());
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cIsUpgrade: %d\n", client->num, client->cIsUpgrade); DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cIsUpgrade: %d\n", client->num, client->cIsUpgrade);
@ -821,7 +803,6 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
} }
if(ok) { if(ok) {
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] Websocket connection incoming.\n", client->num); DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] Websocket connection incoming.\n", client->num);
// generate Sec-WebSocket-Accept key // generate Sec-WebSocket-Accept key
@ -831,7 +812,8 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
client->status = WSC_CONNECTED; client->status = WSC_CONNECTED;
String handshake = WEBSOCKETS_STRING("HTTP/1.1 101 Switching Protocols\r\n" String handshake = WEBSOCKETS_STRING(
"HTTP/1.1 101 Switching Protocols\r\n"
"Server: arduino-WebSocketsServer\r\n" "Server: arduino-WebSocketsServer\r\n"
"Upgrade: websocket\r\n" "Upgrade: websocket\r\n"
"Connection: Upgrade\r\n" "Connection: Upgrade\r\n"
@ -868,6 +850,3 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
} }
} }
} }

View File

@ -31,12 +31,8 @@
#define WEBSOCKETS_SERVER_CLIENT_MAX (5) #define WEBSOCKETS_SERVER_CLIENT_MAX (5)
#endif #endif
class WebSocketsServer : protected WebSockets { class WebSocketsServer : protected WebSockets {
public: public:
#ifdef __AVR__ #ifdef __AVR__
typedef void (*WebSocketServerEvent)(uint8_t num, WStype_t type, uint8_t * payload, size_t length); typedef void (*WebSocketServerEvent)(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
typedef bool (*WebSocketServerHttpHeaderValFunc)(String headerName, String headerValue); typedef bool (*WebSocketServerHttpHeaderValFunc)(String headerName, String headerValue);
@ -64,7 +60,6 @@ public:
const char * mandatoryHttpHeaders[], const char * mandatoryHttpHeaders[],
size_t mandatoryHttpHeaderCount); size_t mandatoryHttpHeaderCount);
bool sendTXT(uint8_t num, uint8_t * payload, size_t length = 0, bool headerToPayload = false); bool sendTXT(uint8_t num, uint8_t * payload, size_t length = 0, bool headerToPayload = false);
bool sendTXT(uint8_t num, const uint8_t * payload, size_t length = 0); bool sendTXT(uint8_t num, const uint8_t * payload, size_t length = 0);
bool sendTXT(uint8_t num, char * payload, size_t length = 0, bool headerToPayload = false); bool sendTXT(uint8_t num, char * payload, size_t length = 0, bool headerToPayload = false);
@ -139,7 +134,8 @@ protected:
*/ */
virtual void handleNonWebsocketConnection(WSclient_t * client) { virtual void handleNonWebsocketConnection(WSclient_t * client) {
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num); DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num);
client->tcp->write("HTTP/1.1 400 Bad Request\r\n" client->tcp->write(
"HTTP/1.1 400 Bad Request\r\n"
"Server: arduino-WebSocket-Server\r\n" "Server: arduino-WebSocket-Server\r\n"
"Content-Type: text/plain\r\n" "Content-Type: text/plain\r\n"
"Content-Length: 32\r\n" "Content-Length: 32\r\n"
@ -156,7 +152,8 @@ protected:
* @param client WSclient_t * ptr to the client struct * @param client WSclient_t * ptr to the client struct
*/ */
virtual void handleAuthorizationFailed(WSclient_t * client) { virtual void handleAuthorizationFailed(WSclient_t * client) {
client->tcp->write("HTTP/1.1 401 Unauthorized\r\n" client->tcp->write(
"HTTP/1.1 401 Unauthorized\r\n"
"Server: arduino-WebSocket-Server\r\n" "Server: arduino-WebSocket-Server\r\n"
"Content-Type: text/plain\r\n" "Content-Type: text/plain\r\n"
"Content-Length: 45\r\n" "Content-Length: 45\r\n"
@ -204,9 +201,6 @@ private:
* @param headerName String ///< the name of the header being checked * @param headerName String ///< the name of the header being checked
*/ */
bool hasMandatoryHeader(String headerName); bool hasMandatoryHeader(String headerName);
}; };
#endif /* WEBSOCKETSSERVER_H_ */ #endif /* WEBSOCKETSSERVER_H_ */