diff --git a/include/WsServer.h b/include/WsServer.h index c9de7fe2..eed0ac78 100644 --- a/include/WsServer.h +++ b/include/WsServer.h @@ -20,4 +20,5 @@ void periodicWsSend(); void sendFileToWsByFrames(const String& filename, const String& header, const String& json, int client_id, size_t frameSize); void sendStringToWs(const String& header, String& payload, int client_id); -void sendDeviceList(uint8_t num); \ No newline at end of file +void sendDeviceList(uint8_t num); +int getNumWSClients(); \ No newline at end of file diff --git a/include/utils/WiFiUtils.h b/include/utils/WiFiUtils.h index 89bec1f4..7959fdc3 100644 --- a/include/utils/WiFiUtils.h +++ b/include/utils/WiFiUtils.h @@ -3,8 +3,8 @@ #include "Global.h" #include "MqttClient.h" -// boolean isNetworkActive(); -inline boolean isNetworkActive() {return WiFi.status() == WL_CONNECTED;}; +boolean isNetworkActive(); +uint8_t getNumAPClients(); void routerConnect(); bool startAPMode(); boolean RouterFind(String ssid); diff --git a/src/WsServer.cpp b/src/WsServer.cpp index 7dd5c8ed..2fbdaf85 100644 --- a/src/WsServer.cpp +++ b/src/WsServer.cpp @@ -414,7 +414,9 @@ void sendFileToWsByFrames(const String& filename, const String& header, const St } void sendStringToWs(const String& header, String& payload, int client_id) { - if (!(WiFi.softAPgetStationNum() || isNetworkActive())) { + if ((!getNumAPClients() && !isNetworkActive()) || !getNumWSClients()) { + // standWebSocket.disconnect(); // это и ниже надо сделать при - + // standWebSocket.close(); // - отключении AP И WiFi(STA), надо менять ядро WiFi. Сейчас не закрывается сессия клиента при пропаже AP И WiFi(STA) return; } @@ -446,3 +448,7 @@ void sendDeviceList(uint8_t num) { SerialPrint("i", "FS", "flash list"); } } + +int getNumWSClients() { + return standWebSocket.connectedClients(false); +} \ No newline at end of file diff --git a/src/utils/SerialPrint.cpp b/src/utils/SerialPrint.cpp index 4b3d1434..879405da 100644 --- a/src/utils/SerialPrint.cpp +++ b/src/utils/SerialPrint.cpp @@ -11,11 +11,11 @@ void SerialPrint(const String& errorLevel, const String& module, const String& m tosend += msg; Serial.println(tosend); - if (isNetworkActive()) { - if (jsonReadInt(settingsFlashJson, F("log")) != 0) { - sendStringToWs(F("corelg"), tosend, -1); - } + // if (isNetworkActive()) { // все проверки происходят в sendStringToWs() + if (jsonReadInt(settingsFlashJson, F("log")) != 0) { + sendStringToWs(F("corelg"), tosend, -1); } + // } if (errorLevel == "E") { cleanString(tosend); diff --git a/src/utils/WiFiUtils.cpp b/src/utils/WiFiUtils.cpp index 681d2c8a..25c0e331 100644 --- a/src/utils/WiFiUtils.cpp +++ b/src/utils/WiFiUtils.cpp @@ -115,9 +115,13 @@ boolean RouterFind(String ssid) { return res; } -// boolean isNetworkActive() { -// return WiFi.status() == WL_CONNECTED; -// } +boolean isNetworkActive() { + return WiFi.status() == WL_CONNECTED; +} + +uint8_t getNumAPClients() { + return WiFi.softAPgetStationNum(); +} uint8_t RSSIquality() { uint8_t res = 0;