Enhance WebSocket server connection management and error handling

- Introduce connection time tracking for WebSocket users to monitor connection duration.
- Implement user ID management to allow dynamic updates and removal of connections based on user ID changes.
- Add functionality to retrieve active connections, including unauthenticated ones, for administrative purposes.
- Improve error handling during connection closure and ensure proper cleanup of connection entries.
This commit is contained in:
Torsten Schulz (local)
2025-11-20 16:16:09 +01:00
committed by Torsten (PC)
parent 753c5929e1
commit 5ac8e9b484
3 changed files with 277 additions and 20 deletions

View File

@@ -22,6 +22,7 @@ struct WebSocketUserData {
bool pongReceived = true;
std::queue<std::string> messageQueue;
std::mutex messageQueueMutex;
std::chrono::steady_clock::time_point connectionTime; // Zeitpunkt der Verbindungsherstellung
std::chrono::steady_clock::time_point lastPingTime;
std::chrono::steady_clock::time_point lastPongTime;
int pingTimeoutCount = 0;
@@ -48,8 +49,12 @@ private:
void pingClients();
void handleBrokerMessage(const std::string &message);
std::string getUserIdFromFalukantUserId(int falukantUserId);
bool isMainAdmin(const std::string &hashedUserId);
json getActiveConnections();
void sendMessageToConnection(struct lws *wsi, 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)
static int wsCallback(struct lws *wsi,
enum lws_callback_reasons reason,
@@ -74,6 +79,7 @@ private:
std::shared_mutex connectionsMutex;
std::unordered_map<std::string, std::vector<struct lws*>> connections;
std::vector<struct lws*> allConnections; // Alle aktiven Verbindungen (auch ohne userId)
std::vector<Worker*> workers;