Skip to content

Need a way to identify the clients of a WebsocketServer #472

Closed

Description

@RnMss

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.

Activity

  1. bsergean commented on Jul 1, 2023

    @bsergean
    Collaborator

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

  2. RnMss commented on Jul 4, 2024

    @RnMss
    Author

    @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.

  3. RnMss commented on Jul 4, 2024

    @RnMss
    Author

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

  4. bsergean commented on Nov 17, 2026

    @bsergean
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions