diff --git a/platformio.ini b/platformio.ini index 0a6f61a0..2acf07c6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,7 +10,7 @@ lib_deps = https://github.com/bblanchon/ArduinoJson.git ; https://github.com/ewowi/ESPAsyncWebServer.git ;me-no-dev + 64 + queueLength - https://github.com/ewoudwijma/ESPAsyncWebServer.git ;aircoookie + getClients + https://github.com/jeremypoulter/ArduinoMongoose.git#19da595 ; v0.0.20 ; https://github.com/pbolduc/AsyncTCP.git @ 1.2.0 ;optionally combine with aircoookie ; https://github.com/lost-hope/ESPAsyncWebServer.git#master ;artifx on /edit diff --git a/src/Sys/SysModPrint.cpp b/src/Sys/SysModPrint.cpp index bf66b7fb..4fffb6c6 100644 --- a/src/Sys/SysModPrint.cpp +++ b/src/Sys/SysModPrint.cpp @@ -144,8 +144,8 @@ void SysModPrint::printJDocInfo(const char * text, DynamicJsonDocument source) { print("%s %u / %u (%u%%) (%u %u %u)\n", text, source.memoryUsage(), source.capacity(), percentage, source.size(), source.overflowed(), source.nesting()); } -void SysModPrint::printClient(const char * text, AsyncWebSocketClient * client) { - print("%s client: %d ...%d q:%d l:%d s:%d (#:%d)\n", text, client?client->id():-1, client?client->remoteIP()[3]:-1, client->queueIsFull(), client->queueLength(), client->status(), client->server()->count()); +void SysModPrint::printClient(const char * text, MongooseHttpWebSocketConnection * client) { + // TODO: print("%s client: %d ...%d q:%d l:%d s:%d (#:%d)\n", text, client?client->id():-1, client?client->remoteIP()[3]:-1, client->queueIsFull(), client->queueLength(), client->status(), client->server()->count()); //status: { WS_DISCONNECTED, WS_CONNECTED, WS_DISCONNECTING } } diff --git a/src/Sys/SysModPrint.h b/src/Sys/SysModPrint.h index 13dc16c1..c77ec624 100644 --- a/src/Sys/SysModPrint.h +++ b/src/Sys/SysModPrint.h @@ -11,7 +11,7 @@ #pragma once #include "SysModule.h" -#include +#include #define USER_PRINTF(x...) print->print(x) #define USER_PRINT_FUNCTION(x...) //print->print(x) @@ -39,7 +39,7 @@ class SysModPrint:public SysModule { void printJDocInfo(const char * text, DynamicJsonDocument source); - void printClient(const char * text, AsyncWebSocketClient * client); + void printClient(const char * text, MongooseHttpWebSocketConnection * client); private: bool setupsDone = false; diff --git a/src/Sys/SysModUI.cpp b/src/Sys/SysModUI.cpp index dcadc62d..4b3b0c13 100644 --- a/src/Sys/SysModUI.cpp +++ b/src/Sys/SysModUI.cpp @@ -106,13 +106,14 @@ void SysModUI::loop() { varLoop->interval = buffer[3]*10; //from cs to ms // USER_PRINTF("interval2 %u %d %d %d %d %d %d\n", millis(), varLoop->interval, varLoop->bufSize, buffer[0], buffer[1], buffer[2], buffer[3]); - for (auto client:SysModWeb::ws->getClients()) { - if (client->status() == WS_CONNECTED && !client->queueIsFull() && client->queueLength()<=1) //lossy - client->binary(wsBuf); - else { - // web->clientsChanged = true; tbd: changed also if full status changes - // print->printClient("loopFun skip frame", client); - } + server->sendAdd(wsBuf); // TODO: needs to send binary not text + // for (auto client:SysModWeb::ws->getClients()) { + // if (client->status() == WS_CONNECTED && !client->queueIsFull() && client->queueLength()<=1) //lossy + // client->binary(wsBuf); + // else { + // // web->clientsChanged = true; tbd: changed also if full status changes + // // print->printClient("loopFun skip frame", client); + // } } wsBuf->unlock(); SysModWeb::ws->_cleanBuffers(); diff --git a/src/Sys/SysModWeb.cpp b/src/Sys/SysModWeb.cpp index 1079a134..9ee5cb7c 100644 --- a/src/Sys/SysModWeb.cpp +++ b/src/Sys/SysModWeb.cpp @@ -360,7 +360,7 @@ void SysModWeb::wsEvent2(AsyncWebSocket * server, AsyncWebSocketClient * client, } -void SysModWeb::sendDataWs(JsonVariant json, AsyncWebSocketClient * client) { +void SysModWeb::sendDataWs(JsonVariant json, MongooseHttpWebSocketConnection * client) { if (!ws) { USER_PRINT_Async("sendDataWs no ws\n"); return; @@ -384,20 +384,21 @@ void SysModWeb::sendDataWs(JsonVariant json, AsyncWebSocketClient * client) { wsBuf->lock(); serializeJson(json, (char *)wsBuf->get(), len); if (client) { - if (client->status() == WS_CONNECTED && !client->queueIsFull()) - client->text(wsBuf); - else - print->printClient("sendDataWs client full or not connected", client); + // if (client->status() == WS_CONNECTED && !client->queueIsFull()) + client->send(wsBuf); + // else + // print->printClient("sendDataWs client full or not connected", client); // DEBUG_PRINTLN("to a single client."); } else { - for (auto client:ws->getClients()) { - if (client->status() == WS_CONNECTED && !client->queueIsFull()) - client->text(wsBuf); - else - // printClient("sendDataWs client(s) full or not connected", client); //crash!! - USER_PRINT_Async("sendDataWs client full or not connected\n"); - } + server->sendAll(wsBuf); + // for (auto client:ws->getClients()) { + // if (client->status() == WS_CONNECTED && !client->queueIsFull()) + // client->text(wsBuf); + // else + // // printClient("sendDataWs client(s) full or not connected", client); //crash!! + // USER_PRINT_Async("sendDataWs client full or not connected\n"); + // } // DEBUG_PRINTLN("to multiple clients."); } wsBuf->unlock(); @@ -626,7 +627,7 @@ JsonDocument * SysModWeb::getResponseDoc() { return strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) == 0?web->responseDocLoopTask:web->responseDocAsyncTCP; } -void SysModWeb::serveJson(AsyncWebServerRequest *request) { +void SysModWeb::serveJson(MongooseHttpServerRequest *request) { AsyncJsonResponse * response; diff --git a/src/Sys/SysModWeb.h b/src/Sys/SysModWeb.h index 668ef3c4..59e055e1 100644 --- a/src/Sys/SysModWeb.h +++ b/src/Sys/SysModWeb.h @@ -11,12 +11,12 @@ #pragma once #include "SysModule.h" -#include +#include class SysModWeb:public SysModule { public: - static AsyncWebSocket *ws; + // static AsyncWebSocket *ws; SysModWeb(); @@ -28,17 +28,16 @@ class SysModWeb:public SysModule { void connectedChanged(); - static void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len); - static void wsEvent2(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len); + // static void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len); + // static void wsEvent2(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len); //send json to client or all clients - static void sendDataWs(JsonVariant json = JsonVariant(), AsyncWebSocketClient * client = nullptr); + static void sendDataWs(JsonVariant json = JsonVariant(), MongooseHttpWebSocketConnection * client = nullptr); //add an url to the webserver to listen to bool addURL(const char * uri, const char * contentType, const char * path = nullptr, const uint8_t * content = nullptr, size_t len = -1); - //not used at the moment - bool processURL(const char * uri, void (*func)(AsyncWebServerRequest *)); + bool processURL(const char * uri, void (*func)(MongooseHttpServerRequest *)); // curl -F 'data=@fixture1.json' 192.168.8.213/upload bool addUpload(const char * uri); @@ -71,7 +70,7 @@ class SysModWeb:public SysModule { JsonDocument * getResponseDoc(); //Currently only WLED style state and info - static void serveJson(AsyncWebServerRequest *request); + static void serveJson(MongooseHttpServerRequest *request); static unsigned long wsSendBytesCounter; static unsigned long wsSendJsonCounter; @@ -81,7 +80,7 @@ class SysModWeb:public SysModule { static bool clientsChanged; - static AsyncWebServer *server; + static MongooseHttpServer *server; static const char * (*processWSFunc)(JsonVariant &); static DynamicJsonDocument *responseDocLoopTask;