From 401e6739cdeb1b11ef6aee884f55ba3ec90b0671 Mon Sep 17 00:00:00 2001 From: Axel Hinrichs Date: Tue, 5 Jul 2022 23:42:07 +0200 Subject: [PATCH] [WebApi] rate limit ws cleanup (1/s) --- include/WebApi.h | 1 + src/WebApi.cpp | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/WebApi.h b/include/WebApi.h index 98ed044..2d82839 100644 --- a/include/WebApi.h +++ b/include/WebApi.h @@ -31,6 +31,7 @@ private: WebApiSysstatusClass _webApiSysstatus; WebApiWebappClass _webApiWebapp; WebApiWsLiveClass _webApiWsLive; + unsigned long lastTimerCall = 0; void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len); }; diff --git a/src/WebApi.cpp b/src/WebApi.cpp index 52ac0df..0194ff7 100644 --- a/src/WebApi.cpp +++ b/src/WebApi.cpp @@ -47,7 +47,10 @@ void WebApiClass::loop() _webApiWsLive.loop(); // see: https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients - _ws.cleanupClients(); + if (millis() - lastTimerCall > 1000) { + _ws.cleanupClients(); + lastTimerCall = millis(); + } } void WebApiClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)