Refactor WebSocket server message handling to include user data
- Update sendMessageToConnection to accept user data, enhancing message delivery accuracy. - Improve error handling in WebSocket callbacks by adding user data checks to prevent null pointer exceptions. - Enhance logging for error responses to provide clearer insights into message handling issues.
This commit is contained in:
committed by
Torsten (PC)
parent
88f6686809
commit
0c36c4a4e5
@@ -328,11 +328,13 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
||||
{"success", false},
|
||||
{"error", "User-ID nicht gesetzt"}
|
||||
};
|
||||
if (instance && wsi) {
|
||||
instance->sendMessageToConnection(wsi, errorResponse.dump());
|
||||
if (instance && wsi && ud) {
|
||||
instance->sendMessageToConnection(wsi, ud, errorResponse.dump());
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "[RECEIVE] Fehler beim Senden der Fehlerantwort: " << e.what() << std::endl;
|
||||
} catch (...) {
|
||||
std::cerr << "[RECEIVE] Unbekannter Fehler beim Senden der Fehlerantwort" << std::endl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -347,8 +349,8 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
||||
{"success", false},
|
||||
{"error", "Zugriff verweigert: Nur Mainadmin-User können Verbindungen abfragen"}
|
||||
};
|
||||
if (instance && wsi) {
|
||||
instance->sendMessageToConnection(wsi, errorResponse.dump());
|
||||
if (instance && wsi && ud) {
|
||||
instance->sendMessageToConnection(wsi, ud, errorResponse.dump());
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -361,10 +363,10 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
||||
{"success", true},
|
||||
{"data", connections}
|
||||
};
|
||||
if (instance && wsi) {
|
||||
if (instance && wsi && ud) {
|
||||
// Sende die Nachricht, aber nicht während des Callbacks
|
||||
// Die Nachricht wird in die Queue gelegt und beim nächsten WRITEABLE gesendet
|
||||
instance->sendMessageToConnection(wsi, response.dump());
|
||||
instance->sendMessageToConnection(wsi, ud, response.dump());
|
||||
std::cout << "[RECEIVE] getConnections: Verbindungen an Mainadmin gesendet (" << response.dump().length() << " Bytes)" << std::endl;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
@@ -376,8 +378,8 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
||||
{"success", false},
|
||||
{"error", std::string("Fehler beim Abrufen der Verbindungen: ") + e.what()}
|
||||
};
|
||||
if (instance && wsi) {
|
||||
instance->sendMessageToConnection(wsi, errorResponse.dump());
|
||||
if (instance && wsi && ud) {
|
||||
instance->sendMessageToConnection(wsi, ud, errorResponse.dump());
|
||||
}
|
||||
} catch (...) {
|
||||
// Ignoriere Fehler beim Senden der Fehlerantwort
|
||||
@@ -728,17 +730,31 @@ void WebSocketServer::sendMessageToConnection(struct lws *wsi, const std::string
|
||||
return;
|
||||
}
|
||||
|
||||
if (!context) {
|
||||
std::cerr << "[sendMessageToConnection] context ist nullptr" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto* ud = reinterpret_cast<WebSocketUserData*>(lws_wsi_user(wsi));
|
||||
if (!ud) {
|
||||
std::cerr << "[sendMessageToConnection] ud ist nullptr" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
sendMessageToConnection(wsi, ud, message);
|
||||
}
|
||||
|
||||
void WebSocketServer::sendMessageToConnection(struct lws *wsi, WebSocketUserData *ud, const std::string &message) {
|
||||
if (!wsi) {
|
||||
std::cerr << "[sendMessageToConnection] wsi ist nullptr" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ud) {
|
||||
std::cerr << "[sendMessageToConnection] ud ist nullptr" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!context) {
|
||||
std::cerr << "[sendMessageToConnection] context ist nullptr" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
bool wasEmpty = false;
|
||||
{
|
||||
|
||||
@@ -52,6 +52,7 @@ private:
|
||||
bool isMainAdmin(const std::string &hashedUserId);
|
||||
nlohmann::json getActiveConnections();
|
||||
void sendMessageToConnection(struct lws *wsi, const std::string &message);
|
||||
void sendMessageToConnection(struct lws *wsi, WebSocketUserData *ud, const std::string &message);
|
||||
void addConnection(const std::string &userId, struct lws *wsi);
|
||||
void removeConnection(const std::string &userId, struct lws *wsi);
|
||||
void removeConnectionByWsi(struct lws *wsi); // Entfernt Verbindung aus allen Einträgen (Fallback)
|
||||
|
||||
Reference in New Issue
Block a user