IXWebSocket icon indicating copy to clipboard operation
IXWebSocket copied to clipboard

Need a way to identify the clients of a WebsocketServer

Open RnMss opened this issue 2 years ago • 3 comments

For now I haven't found a way to match a Websocket& with the std::set<std::shared_ptr<WebSocket>>, from the client message callback void(std::shared_ptr<ConnectionState>, WebSocket&, const WebSocketMessagePtr&), and get clients std::set<std::shared_ptr<WebSocket>> getClients() respectively. I can not tell the clients from getClients from each other, this is necessary when you want to send message asynchronously.

For now the only workaround I know is address comparison, which is not a real solution. Since there is an unegligible chance that a newly allocated Websocket object takes the place of a previously free-d one (in the heap), so they'll have the same address.

I guess this can be done by changing the OnClientMessageCallback to std::function<void(std::shared_ptr<ConnectionState>, std::shared_ptr<WebSocket>, const WebSocketMessagePtr&)> (replace reference with shared_ptr). The shared_ptr can be converted to a weak_ptr for long-term storage. This is a breaking change, though.

RnMss avatar Jun 16 '23 03:06 RnMss

I think the real way is to have client connection authenticate, and then associate them with a user id or equivalent.

bsergean avatar Jul 01 '23 00:07 bsergean

@bsergean The problem is still there. How do you associate a WebSocket object with anything such as a user id?

I read the code and it seems I have to use the legacy API. I'll need to maintain a list/map/set of std::weak_ptr<WebSocket> so I can find the client when I want to send a message to a particular user.

RnMss avatar Jul 04 '24 04:07 RnMss

I read further and I think I'll just maintain a list/map of WebSocket* (raw pointer)

RnMss avatar Jul 04 '24 08:07 RnMss