Enhance error handling and logging in WebSocket server message sending

- Introduce detailed logging for message creation and sending processes, including message size and success confirmation.
- Implement comprehensive null checks for instance, WebSocket interface, and user data before invoking sendMessageToConnection, improving stability.
- Add exception handling to capture and log errors during message sending, enhancing visibility into potential issues.
This commit is contained in:
Torsten Schulz (local)
2025-11-20 17:55:18 +01:00
committed by Torsten (PC)
parent 45d549aa4e
commit 2595cb8565

View File

@@ -366,8 +366,34 @@ int WebSocketServer::wsCallback(struct lws *wsi,
}
// Lege Nachricht in die Queue, ohne sofort lws_callback_on_writable aufzurufen
// Verwende sendMessageToConnection, das bereits alle notwendigen Prüfungen hat
instance->sendMessageToConnection(wsi, ud, errorResponse.dump());
// Kopiere die Nachricht und verwende sendMessageToConnection
try {
std::cout << "[RECEIVE] Erstelle messageStr..." << std::endl;
std::string messageStr = errorResponse.dump();
std::cout << "[RECEIVE] messageStr erstellt: " << messageStr.length() << " Bytes" << std::endl;
// Prüfe ob instance, wsi und ud noch gültig sind
if (!instance) {
std::cerr << "[RECEIVE] instance ist nullptr vor sendMessageToConnection" << std::endl;
break;
}
if (!wsi) {
std::cerr << "[RECEIVE] wsi ist nullptr vor sendMessageToConnection" << std::endl;
break;
}
if (!ud) {
std::cerr << "[RECEIVE] ud ist nullptr vor sendMessageToConnection" << std::endl;
break;
}
std::cout << "[RECEIVE] Rufe sendMessageToConnection auf..." << std::endl;
instance->sendMessageToConnection(wsi, ud, messageStr);
std::cout << "[RECEIVE] sendMessageToConnection erfolgreich aufgerufen" << std::endl;
} catch (const std::exception &e) {
std::cerr << "[RECEIVE] Exception beim Aufruf von sendMessageToConnection: " << e.what() << std::endl;
} catch (...) {
std::cerr << "[RECEIVE] Unbekannte Exception beim Aufruf von sendMessageToConnection" << std::endl;
}
// Verwende lws_cancel_service, um den Service zu benachrichtigen
if (instance->context) {