From 2a26a97e3a18b143c635fdf8bf7e67327df8ea48 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Thu, 19 Dec 2024 13:12:59 +0900 Subject: [PATCH 1/4] move AsyncWebSocket's WS_EVT_CONNECT callback out of AsyncWebSocketClient's constructor - this is wrong place to call user code from an object constructor - it is wrong to call AsyncWebSocket's method from other's objects constructor - the callback is executed when new object is not yet linked to server's clients list Closes #176 --- src/AsyncWebSocket.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AsyncWebSocket.cpp b/src/AsyncWebSocket.cpp index 13e6add..9deb74a 100644 --- a/src/AsyncWebSocket.cpp +++ b/src/AsyncWebSocket.cpp @@ -287,7 +287,6 @@ AsyncWebSocketClient::AsyncWebSocketClient(AsyncWebServerRequest* request, Async _client->onTimeout([](void* r, AsyncClient* c, uint32_t time) { (void)c; ((AsyncWebSocketClient*)(r))->_onTimeout(time); }, this); _client->onData([](void* r, AsyncClient* c, void* buf, size_t len) { (void)c; ((AsyncWebSocketClient*)(r))->_onData(buf, len); }, this); _client->onPoll([](void* r, AsyncClient* c) { (void)c; ((AsyncWebSocketClient*)(r))->_onPoll(); }, this); - _server->_handleEvent(this, WS_EVT_CONNECT, request, NULL, 0); delete request; memset(&_pinfo, 0, sizeof(_pinfo)); } @@ -781,6 +780,7 @@ void AsyncWebSocket::_handleEvent(AsyncWebSocketClient* client, AwsEventType typ AsyncWebSocketClient* AsyncWebSocket::_newClient(AsyncWebServerRequest* request) { _clients.emplace_back(request, this); + _handleEvent(&_clients.back(), WS_EVT_CONNECT, request, NULL, 0); return &_clients.back(); } From 0554cee7a9b7d8feb647438d51b8a31e090b51e5 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Thu, 19 Dec 2024 09:40:30 +0100 Subject: [PATCH 2/4] Added example to demo that fix #177 fixes issue #176 --- examples/SimpleServer/SimpleServer.ino | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/SimpleServer/SimpleServer.ino b/examples/SimpleServer/SimpleServer.ino index 3573827..7c09ccc 100644 --- a/examples/SimpleServer/SimpleServer.ino +++ b/examples/SimpleServer/SimpleServer.ino @@ -147,6 +147,8 @@ AsyncMiddlewareFunction complexAuth([](AsyncWebServerRequest* request, ArMiddlew AuthorizationMiddleware authz([](AsyncWebServerRequest* request) { return request->getAttribute("role") == "staff"; }); +int wsClients = 0; + ///////////////////////////////////////////////////////////////////////////////////////////////////// const char* PARAM_MESSAGE PROGMEM = "message"; @@ -646,10 +648,14 @@ void setup() { ws.onEvent([](AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) { (void)len; if (type == WS_EVT_CONNECT) { + wsClients++; + ws.textAll("new client connected"); Serial.println("ws connect"); client->setCloseClientOnQueueFull(false); client->ping(); } else if (type == WS_EVT_DISCONNECT) { + wsClients--; + ws.textAll("client disconnected"); Serial.println("ws disconnect"); } else if (type == WS_EVT_ERROR) { Serial.println("ws error"); From 18b3dd9c0374ff7dfcc37fe83ba593debae645e0 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Thu, 19 Dec 2024 10:05:48 +0100 Subject: [PATCH 3/4] Display heap --- examples/SimpleServer/SimpleServer.ino | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/SimpleServer/SimpleServer.ino b/examples/SimpleServer/SimpleServer.ino index 7c09ccc..f2b3c0a 100644 --- a/examples/SimpleServer/SimpleServer.ino +++ b/examples/SimpleServer/SimpleServer.ino @@ -766,6 +766,8 @@ uint32_t deltaSSE = 10; uint32_t lastWS = 0; uint32_t deltaWS = 100; +uint32_t lastHeap = 0; + void loop() { uint32_t now = millis(); if (now - lastSSE >= deltaSSE) { @@ -779,4 +781,8 @@ void loop() { // } lastWS = millis(); } + if(now - lastHeap >= 2000) { + Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap()); + lastHeap = now; + } } From c1be1c44ee3781faf0d29d8645d5295d8c3377ba Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Thu, 19 Dec 2024 10:06:06 +0100 Subject: [PATCH 4/4] disable colors --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index d302ae2..cd19455 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,7 +14,7 @@ build_flags = -Og -Wall -Wextra -Wno-unused-parameter - -D CONFIG_ARDUHAL_LOG_COLORS + ; -D CONFIG_ARDUHAL_LOG_COLORS -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE -D CONFIG_ASYNC_TCP_MAX_ACK_TIME=5000 -D CONFIG_ASYNC_TCP_PRIORITY=10