diff --git a/src/WebSockets.h b/src/WebSockets.h index 91d95eb..00d65b2 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -259,34 +259,46 @@ typedef struct { } WSMessageHeader_t; typedef struct { - uint8_t num; ///< connection number - WSclientsStatus_t status; + void init (uint8_t num, + uint32_t pingInterval, + uint32_t pongTimeout, + uint8_t disconnectTimeoutCount) + { + this->num = num; + this->pingInterval = pingInterval; + this->pongTimeout = pongTimeout; + this->disconnectTimeoutCount = disconnectTimeoutCount; + } + + uint8_t num = 0; ///< connection number + + WSclientsStatus_t status = WSC_NOT_CONNECTED; - WEBSOCKETS_NETWORK_CLASS * tcp; + WEBSOCKETS_NETWORK_CLASS * tcp = nullptr; - bool isSocketIO; ///< client for socket.io server + bool isSocketIO = false; ///< client for socket.io server #if defined(HAS_SSL) - bool isSSL; ///< run in ssl mode + bool isSSL = false; ///< run in ssl mode WEBSOCKETS_NETWORK_SSL_CLASS * ssl; #endif - String cUrl; ///< http url - uint16_t cCode; ///< http code + String cUrl; ///< http url + uint16_t cCode = 0; ///< http code bool cIsClient = false; ///< will be used for masking - bool cIsUpgrade; ///< Connection == Upgrade - bool cIsWebsocket; ///< Upgrade == websocket + bool cIsUpgrade = false; ///< Connection == Upgrade + bool cIsWebsocket = false; ///< Upgrade == websocket String cSessionId; ///< client Set-Cookie (session id) String cKey; ///< client Sec-WebSocket-Key String cAccept; ///< client Sec-WebSocket-Accept String cProtocol; ///< client Sec-WebSocket-Protocol String cExtensions; ///< client Sec-WebSocket-Extensions - uint16_t cVersion; ///< client Sec-WebSocket-Version + uint16_t cVersion = 0; ///< client Sec-WebSocket-Version - uint8_t cWsRXsize; ///< State of the RX + uint8_t cWsRXsize = 0; ///< State of the RX uint8_t cWsHeader[WEBSOCKETS_MAX_HEADER_SIZE]; ///< RX WS Message buffer WSMessageHeader_t cWsHeaderDecode; @@ -295,15 +307,15 @@ typedef struct { String extraHeaders; - bool cHttpHeadersValid; ///< non-websocket http header validity indicator + bool cHttpHeadersValid = false; ///< non-websocket http header validity indicator size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count - bool pongReceived; - uint32_t pingInterval; // how often ping will be sent, 0 means "heartbeat is not active" - uint32_t lastPing; // millis when last pong has been received - uint32_t pongTimeout; // interval in millis after which pong is considered to timeout - uint8_t disconnectTimeoutCount; // after how many subsequent pong timeouts discconnect will happen, 0 means "do not disconnect" - uint8_t pongTimeoutCount; // current pong timeout count + bool pongReceived = false; + uint32_t pingInterval = 0; // how often ping will be sent, 0 means "heartbeat is not active" + uint32_t lastPing = 0; // millis when last pong has been received + uint32_t pongTimeout = 0; // interval in millis after which pong is considered to timeout + uint8_t disconnectTimeoutCount = 0; // after how many subsequent pong timeouts discconnect will happen, 0 means "do not disconnect" + uint8_t pongTimeoutCount = 0; // current pong timeout count #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) String cHttpLine; ///< HTTP header lines diff --git a/src/WebSockets4WebServer.h b/src/WebSockets4WebServer.h index 01a7f8a..ba2b020 100644 --- a/src/WebSockets4WebServer.h +++ b/src/WebSockets4WebServer.h @@ -41,6 +41,8 @@ class WebSockets4WebServer : public WebSocketsServerCore { onEvent(event); return [&, wsRootDir](const String & method, const String & url, WiFiClient * tcpClient, ESP8266WebServer::ContentTypeFunction contentType) { + (void)contentType; + if(!(method == "GET" && url.indexOf(wsRootDir) == 0)) { return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE; } diff --git a/src/WebSocketsServer.cpp b/src/WebSocketsServer.cpp index 20b2373..c8ac2ef 100644 --- a/src/WebSocketsServer.cpp +++ b/src/WebSocketsServer.cpp @@ -38,8 +38,6 @@ WebSocketsServerCore::WebSocketsServerCore(const String & origin, const String & _httpHeaderValidationFunc = NULL; _mandatoryHttpHeaders = NULL; _mandatoryHttpHeaderCount = 0; - - memset(&_clients[0], 0x00, (sizeof(WSclient_t) * WEBSOCKETS_SERVER_CLIENT_MAX)); } WebSocketsServer::WebSocketsServer(uint16_t port, const String & origin, const String & protocol) @@ -73,47 +71,14 @@ WebSocketsServer::~WebSocketsServer() { * called to initialize the Websocket server */ void WebSocketsServerCore::begin(void) { - WSclient_t * client; - - // init client storage - for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { - client = &_clients[i]; - - client->num = i; - client->status = WSC_NOT_CONNECTED; - client->tcp = NULL; -#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) - client->isSSL = false; - client->ssl = NULL; -#endif - client->cUrl = ""; - client->cCode = 0; - - client->cIsClient = false; - client->cIsUpgrade = false; - client->cIsWebsocket = false; - client->cSessionId = ""; - client->cKey = ""; - client->cAccept = ""; - client->cProtocol = ""; - client->cExtensions = ""; - client->cVersion = 0; - - client->cWsRXsize = 0; - - client->base64Authorization = ""; - client->plainAuthorization = ""; - - client->extraHeaders = ""; - -#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) - client->cHttpLine = ""; -#endif - - client->pingInterval = _pingInterval; - client->pongTimeout = _pongTimeout; - client->disconnectTimeoutCount = _disconnectTimeoutCount; + // adjust clients storage: + // _clients[i]'s constructor are already called, + // all its members are initialized to their default value, + // except the ones explicitly detailed in WSclient_t() constructor. + // Then we need to initialize some members to non-trivial values: + for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { + _clients[i].init(i, _pingInterval, _pongTimeout, _disconnectTimeoutCount); } #ifdef ESP8266 @@ -132,6 +97,12 @@ void WebSocketsServerCore::begin(void) { void WebSocketsServerCore::close(void) { _runnning = false; disconnect(); + + // restore _clients[] to their initial state + // before next call to ::begin() + for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { + _clients[i] = WSclient_t(); + } } /**