Refactor WebSocket server connection management and message handling

- Update WebSocketUserData to use a message queue for handling outgoing messages, improving concurrency and message delivery.
- Modify pingClients method to handle multiple connections per user and implement timeout logic for ping responses.
- Enhance addConnection and removeConnection methods to manage multiple connections for each user, including detailed logging of connection states.
- Update handleBrokerMessage to send messages to all active connections for a user, ensuring proper queue management and callback invocation.
This commit is contained in:
Torsten Schulz (local)
2025-11-04 15:36:43 +01:00
committed by Torsten (PC)
parent 6a1260687b
commit 00a5f47cae
2 changed files with 108 additions and 48 deletions

View File

@@ -20,7 +20,8 @@
struct WebSocketUserData {
std::string userId;
bool pongReceived = true;
std::string pendingMessage;
std::queue<std::string> messageQueue;
std::mutex messageQueueMutex;
std::chrono::steady_clock::time_point lastPingTime;
std::chrono::steady_clock::time_point lastPongTime;
int pingTimeoutCount = 0;
@@ -48,7 +49,7 @@ private:
void handleBrokerMessage(const std::string &message);
std::string getUserIdFromFalukantUserId(int falukantUserId);
void addConnection(const std::string &userId, struct lws *wsi);
void removeConnection(const std::string &userId);
void removeConnection(const std::string &userId, struct lws *wsi);
static int wsCallback(struct lws *wsi,
enum lws_callback_reasons reason,
@@ -72,7 +73,7 @@ private:
std::queue<std::string> messageQueue;
std::shared_mutex connectionsMutex;
std::unordered_map<std::string, struct lws*> connections;
std::unordered_map<std::string, std::vector<struct lws*>> connections;
std::vector<Worker*> workers;