Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Commit 7f0154c

Browse files
Merge pull request #179 from mathieucarbou/ws_onconnect
move AsyncWebSocket's WS_EVT_CONNECT callback out of AsyncWebSocketClient's constructor
2 parents c2147e9 + 0554cee commit 7f0154c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

examples/SimpleServer/SimpleServer.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ AsyncMiddlewareFunction complexAuth([](AsyncWebServerRequest* request, ArMiddlew
147147

148148
AuthorizationMiddleware authz([](AsyncWebServerRequest* request) { return request->getAttribute("role") == "staff"; });
149149

150+
int wsClients = 0;
151+
150152
/////////////////////////////////////////////////////////////////////////////////////////////////////
151153

152154
const char* PARAM_MESSAGE PROGMEM = "message";
@@ -646,10 +648,14 @@ void setup() {
646648
ws.onEvent([](AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
647649
(void)len;
648650
if (type == WS_EVT_CONNECT) {
651+
wsClients++;
652+
ws.textAll("new client connected");
649653
Serial.println("ws connect");
650654
client->setCloseClientOnQueueFull(false);
651655
client->ping();
652656
} else if (type == WS_EVT_DISCONNECT) {
657+
wsClients--;
658+
ws.textAll("client disconnected");
653659
Serial.println("ws disconnect");
654660
} else if (type == WS_EVT_ERROR) {
655661
Serial.println("ws error");

src/AsyncWebSocket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ AsyncWebSocketClient::AsyncWebSocketClient(AsyncWebServerRequest* request, Async
287287
_client->onTimeout([](void* r, AsyncClient* c, uint32_t time) { (void)c; ((AsyncWebSocketClient*)(r))->_onTimeout(time); }, this);
288288
_client->onData([](void* r, AsyncClient* c, void* buf, size_t len) { (void)c; ((AsyncWebSocketClient*)(r))->_onData(buf, len); }, this);
289289
_client->onPoll([](void* r, AsyncClient* c) { (void)c; ((AsyncWebSocketClient*)(r))->_onPoll(); }, this);
290-
_server->_handleEvent(this, WS_EVT_CONNECT, request, NULL, 0);
291290
delete request;
292291
memset(&_pinfo, 0, sizeof(_pinfo));
293292
}
@@ -781,6 +780,7 @@ void AsyncWebSocket::_handleEvent(AsyncWebSocketClient* client, AwsEventType typ
781780

782781
AsyncWebSocketClient* AsyncWebSocket::_newClient(AsyncWebServerRequest* request) {
783782
_clients.emplace_back(request, this);
783+
_handleEvent(&_clients.back(), WS_EVT_CONNECT, request, NULL, 0);
784784
return &_clients.back();
785785
}
786786

0 commit comments

Comments
 (0)