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},
|
{"success", false},
|
||||||
{"error", "User-ID nicht gesetzt"}
|
{"error", "User-ID nicht gesetzt"}
|
||||||
};
|
};
|
||||||
if (instance && wsi) {
|
if (instance && wsi && ud) {
|
||||||
instance->sendMessageToConnection(wsi, errorResponse.dump());
|
instance->sendMessageToConnection(wsi, ud, errorResponse.dump());
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
std::cerr << "[RECEIVE] Fehler beim Senden der Fehlerantwort: " << e.what() << std::endl;
|
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;
|
break;
|
||||||
}
|
}
|
||||||
@@ -347,8 +349,8 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
|||||||
{"success", false},
|
{"success", false},
|
||||||
{"error", "Zugriff verweigert: Nur Mainadmin-User können Verbindungen abfragen"}
|
{"error", "Zugriff verweigert: Nur Mainadmin-User können Verbindungen abfragen"}
|
||||||
};
|
};
|
||||||
if (instance && wsi) {
|
if (instance && wsi && ud) {
|
||||||
instance->sendMessageToConnection(wsi, errorResponse.dump());
|
instance->sendMessageToConnection(wsi, ud, errorResponse.dump());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -361,10 +363,10 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
|||||||
{"success", true},
|
{"success", true},
|
||||||
{"data", connections}
|
{"data", connections}
|
||||||
};
|
};
|
||||||
if (instance && wsi) {
|
if (instance && wsi && ud) {
|
||||||
// Sende die Nachricht, aber nicht während des Callbacks
|
// Sende die Nachricht, aber nicht während des Callbacks
|
||||||
// Die Nachricht wird in die Queue gelegt und beim nächsten WRITEABLE gesendet
|
// 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;
|
std::cout << "[RECEIVE] getConnections: Verbindungen an Mainadmin gesendet (" << response.dump().length() << " Bytes)" << std::endl;
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
@@ -376,8 +378,8 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
|||||||
{"success", false},
|
{"success", false},
|
||||||
{"error", std::string("Fehler beim Abrufen der Verbindungen: ") + e.what()}
|
{"error", std::string("Fehler beim Abrufen der Verbindungen: ") + e.what()}
|
||||||
};
|
};
|
||||||
if (instance && wsi) {
|
if (instance && wsi && ud) {
|
||||||
instance->sendMessageToConnection(wsi, errorResponse.dump());
|
instance->sendMessageToConnection(wsi, ud, errorResponse.dump());
|
||||||
}
|
}
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
// Ignoriere Fehler beim Senden der Fehlerantwort
|
// Ignoriere Fehler beim Senden der Fehlerantwort
|
||||||
@@ -728,17 +730,31 @@ void WebSocketServer::sendMessageToConnection(struct lws *wsi, const std::string
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!context) {
|
|
||||||
std::cerr << "[sendMessageToConnection] context ist nullptr" << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto* ud = reinterpret_cast<WebSocketUserData*>(lws_wsi_user(wsi));
|
auto* ud = reinterpret_cast<WebSocketUserData*>(lws_wsi_user(wsi));
|
||||||
if (!ud) {
|
if (!ud) {
|
||||||
std::cerr << "[sendMessageToConnection] ud ist nullptr" << std::endl;
|
std::cerr << "[sendMessageToConnection] ud ist nullptr" << std::endl;
|
||||||
return;
|
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 {
|
try {
|
||||||
bool wasEmpty = false;
|
bool wasEmpty = false;
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ private:
|
|||||||
bool isMainAdmin(const std::string &hashedUserId);
|
bool isMainAdmin(const std::string &hashedUserId);
|
||||||
nlohmann::json getActiveConnections();
|
nlohmann::json getActiveConnections();
|
||||||
void sendMessageToConnection(struct lws *wsi, const std::string &message);
|
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 addConnection(const std::string &userId, struct lws *wsi);
|
||||||
void removeConnection(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)
|
void removeConnectionByWsi(struct lws *wsi); // Entfernt Verbindung aus allen Einträgen (Fallback)
|
||||||
|
|||||||
Reference in New Issue
Block a user