Skip to content

Commit

Permalink
fixed websocket reconnect bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Schneider committed Feb 1, 2022
1 parent 30480bb commit 683833a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions data/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ <h2>

// logging
let websocket = undefined;
const logEntries = [];
let logEntries = [];
const maxLogentries = 1000;

document.addEventListener("visibilitychange", onVisibilityChanged, false);
Expand All @@ -137,7 +137,7 @@ <h2>
if (updateTimer === undefined) {
updateTimer = setInterval(function () {
requestRuntimeValues();
if (websocket == null) {
if (websocket == null|| websocket?.readyState != 1 /*OPEN*/ ) {
setupLogging();
}
}, 2500);
Expand Down
4 changes: 2 additions & 2 deletions include/SS2KLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "LogAppender.h"
#include <vector>

#define SS2K_LOG_TAG "SS2K"
#define DEBUG_INFO_LOG_TAG "DebugInfo"
#define SS2K_LOG_TAG "SS2K"
#define LOG_HANDLER_TAG "Log_Handler"

#ifndef DEBUG_LOG_BUFFER_SIZE
#define DEBUG_LOG_BUFFER_SIZE 0
Expand Down
1 change: 0 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ lib_deps =
bblanchon/ArduinoJson@^6.18.5
https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot.git#master
https://github.com/gin66/FastAccelStepper/archive/refs/tags/0.24.0.zip
; https://github.com/Links2004/arduinoWebSockets/archive/refs/tags/2.3.5.zip
https://github.com/gilmaimon/ArduinoWebsockets/archive/refs/tags/0.5.3.zip

[env:release]
Expand Down
17 changes: 11 additions & 6 deletions src/SS2KLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ void LogHandler::addAppender(ILogAppender *appender) { _appenders.push_back(appe

void LogHandler::initialize() {
for (ILogAppender *appender : _appenders) {
appender->Initialize();
try {
appender->Initialize();
} catch (...) {
SS2K_LOG(LOG_HANDLER_TAG, "Fatal error during initialize of log appender.");
}
}
}

Expand All @@ -40,21 +44,22 @@ void LogHandler::writeLogs() {
try {
appender->Log(buffer);
} catch (...) {
ESP_LOGE("LogHandler", "Fatal error during writing to logs.");
SS2K_LOG(LOG_HANDLER_TAG, "Fatal error during writing to log appender.");
}
}
}
ESP_LOGE("LogHandler", "Exit writeLogs(). Messages remaining in buffer.");
SS2K_LOG(LOG_HANDLER_TAG, "Exit writeLogs(). Messages remaining in buffer.");
}

void LogHandler::writev(esp_log_level_t level, const char *module, const char *format, va_list args) {
if (xSemaphoreTake(_logBufferMutex, 10) == pdFALSE) {
ESP_LOGE("LogHandler", "Can not write log message. Write is blocke by other task.");
// Must use ESP_LOG here using of SSK_LOG creates dead lock
ESP_LOGE(LOG_HANDLER_TAG, "Can not write log message. Write is blocke by other task.");
return;
}

if (_messageBufferHandle == NULL) {
ESP_LOGE("LogHandler", "Can not send log message. Message Buffer is NULL");
ESP_LOGE(LOG_HANDLER_TAG, "Can not send log message. Message Buffer is NULL");
return;
}

Expand All @@ -73,7 +78,7 @@ void LogHandler::writev(esp_log_level_t level, const char *module, const char *f
size_t bytesSent = xMessageBufferSend(_messageBufferHandle, buffer, written, 0);

if (bytesSent < written) {
ESP_LOGE("LogHandler", "Can not send log message. Not enough free space left in buffer.");
ESP_LOGE(LOG_HANDLER_TAG, "Can not send log message. Not enough free space left in buffer.");
}

xSemaphoreGive(_logBufferMutex);
Expand Down
1 change: 1 addition & 0 deletions src/WebsocketAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
void WebSocketAppender::Initialize() { _webSocketsServer.listen(WebSocketAppender::port); }
void WebSocketAppender::Loop() {
if (WiFi.status() == WL_CONNECTED && !_client.available()) {
_client.close();
if (_webSocketsServer.poll() == false) {
return;
}
Expand Down

0 comments on commit 683833a

Please sign in to comment.