1
votes

I've read through: http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections

And... I was thinking that by setting up a custom UserIdProvider it would prevent me from having to map connections to real users and then caching it to support multiple servers, but maybe I am missing something.

I can send a message to a specific user: context.Clients.User("xxx").method... no problem.

But, I see no way to tell if a user id has disconnected... as in all connection ids with that user id have been released. This makes the user id provider almost useless, because I am now forced to create my own mapping of connection ids to a unique user id and then remove the connection ids on disconnect, just so I can tell when the list of connection ids associated with a user id is empty. Am I missing something here? I want to be able to tell if the given user id is connected. Is this possible? Ultimately I want to know when they are all disconnected (i.e.: a user is totally disconnected, as in... has closed all tabs with connections).

Something like:

context.Clients.User(x).HasConnections() or context.Clients.User(x).ConnectionCount

I also don't see a way to call GetUserId... I see in other samples Context.User.GetUserId(), but this method does not exist for me. I'm using the latest version of SignalR. My get user id method seems to be called fine from SignalR itself. Right now I have to call it directly rather than through the object model:

    private string GetUserId(HubCallerContext context)
    {
        return new UserIdProvider().GetUserId(Context.Request);
    }
1

1 Answers

0
votes

You can do this if you save the state of connected user in some sort of DB or some other shared storage between your servers Then you can query that DB before trying to send data to that ConnectionId

public class MyHub : Hub
    {
        public override Task OnConnected()
        {
            DAL.UserConnected(Context.ConnectionId);
            return base.OnConnected();
        }
        public override Task OnDisconnected()
        {
            DAL.UserNotConnected(Context.ConnectionId);
            return base.OnDisconnected();
        }
    }