From c72bff5b2e83cebb89d5b9dd1da8b8941b9d4cb0 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Tue, 26 Dec 2017 21:10:54 +0100 Subject: [PATCH] #5 fixing textThem to function properly --- src/ESPUI.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index 8c1d14d..e7c5e6c 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -95,12 +95,17 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, Serial.printf("Disconnected!\n"); break; case WS_EVT_CONNECT: { - if (debug) - Serial.println("Connected"); + if (debug){ + Serial.print("Connected: "); + Serial.println(client->id()); + } + ESPUI.jsonDom(client); - if (debug) + if (debug){ Serial.println("JSON Data Sent to Client!"); - } break; + } + } + break; case WS_EVT_DATA: String msg = ""; for (size_t i = 0; i < len; i++) { @@ -337,11 +342,17 @@ void ESPUIClass::updateSwitcher(String label, bool nValue, int clientId) { updateSwitcher(getIdByLabel(label), nValue, clientId); } +// This is a hacky workaround because ESPAsyncWebServer does not have a function like this and it's clients array is private void ESPUIClass::textThem(String text, int clientId){ - for(int i = 1; i <= this->ws->count(); i++){ - if(clientId!=i){ - this->ws->client(i)->text(text); + int tryId = 0; + for(int count = 0; count < this->ws->count();){ + if(this->ws->hasClient(tryId)) { + if(clientId!=tryId){ + this->ws->client(tryId)->text(text); + } + count++; } + tryId++; } }