You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Need a way to identify the clients of a WebsocketServer #472
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.
@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.
The existing setOnConnectionCallback() API provides the weak handle needed here: its arguments are std::weak_ptr<ix::WebSocket> and std::shared_ptr<ix::ConnectionState>. Register the connection's message callback inside that setup callback, and store its weak pointer in an application registry keyed by connectionState->getId().
For a later send, copy the weak pointer under the registry's mutex, release that mutex, then call lock() and send through the resulting shared pointer if it succeeds. An expired weak pointer cannot attach to a new object at a reused address. This avoids raw-address identity and avoids a shared-pointer cycle in the connection's own callback.
#654 clarifies this supported API and fixes the example's callback signature, delimiters and remote-IP accessor. The corrected example compiles as C++11. Closing as supported through this existing callback; no breaking change to setOnClientMessageCallback() is required.
Activity
bsergean commentedon Jul 1, 2023
I think the real way is to have client connection authenticate, and then associate them with a user id or equivalent.
RnMss commentedon Jul 4, 2024
@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 commentedon Jul 4, 2024
I read further and I think I'll just maintain a list/map of
WebSocket*(raw pointer)bsergean commentedon Nov 17, 2026
The existing
setOnConnectionCallback()API provides the weak handle needed here: its arguments arestd::weak_ptr<ix::WebSocket>andstd::shared_ptr<ix::ConnectionState>. Register the connection's message callback inside that setup callback, and store its weak pointer in an application registry keyed byconnectionState->getId().For a later send, copy the weak pointer under the registry's mutex, release that mutex, then call
lock()and send through the resulting shared pointer if it succeeds. An expired weak pointer cannot attach to a new object at a reused address. This avoids raw-address identity and avoids a shared-pointer cycle in the connection's own callback.#654 clarifies this supported API and fixes the example's callback signature, delimiters and remote-IP accessor. The corrected example compiles as C++11. Closing as supported through this existing callback; no breaking change to
setOnClientMessageCallback()is required.