Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

signalR tracking connected users

I need to check whether a specific user is still connected.

I have the following HashSet that keeps track of users:

public static class UserHandler
{
    public static HashSet<string> ConnectedIds = new HashSet<string>();
}

So on disconnected:

    public override Task OnDisconnected()
    {
        UserHandler.ConnectedIds.Remove(this.Context.ConnectionId);
        if (UserHandler.ConnectedIds.Count == 0)
        {
               // STOP TASK -> SET CANCELLATION
        }
        return base.OnDisconnected();
    }

and on Connected:

    public override Task OnConnected()
    {
        Logger.Log.file.Info("[TaskActionStatus HUB => OnConnected] Start");

        UserHandler.ConnectedIds.Add(this.Context.ConnectionId);

        // start task only if at least one listener
        if (UserHandler.ConnectedIds.Count < 2)
        {
            // START A TASK -> (WHILE LOOP THAT GETS DATA FROM DB EVERY 2 SEC)
        }

        return base.OnConnected();
    }

Now, the problem arises when a users simply closes his/her browser window. There is no way for me to know if he/she disconnected.

So based on some question here, I would call each client and get a response. I'm not sure how to do it.

I guess I would have this:

public static void CheckConnectedUsers()
{
    var context = GlobalHost.ConnectionManager.GetHubContext<TaskActionStatus>();
    foreach (var connection in UserHandler.ConnectedIds)
    {
        context.Clients.Client(connection).verifyConnected("online?");
    }
}

BUT how do I get a response back form the client?

EDIT: steps that are executed:

I need the exact number of users connected because If there is at least one user connected I need to start a task. And I want to ensure that only 1 task is running for all connected users < 2 (start a task). suppose a situation like this: 2 users view a page that is using signalR. HashSet now contains 2 ids and a task is running. One closes his/her browser and after a few seconds and another refreshes his/hers. Now, the first one (the one that closed his/her browser did not execute OnDisconnected method so HashSet still has his/her ConnectionId, and the one that "refreshed" did execute OnDisonnected and then OnConnected which added his connectionId to HashSet. So instead of having only 1 ConnectionId in a HashSet and starting a task, HashSet contains 2 (old-not connected anymore and the one that just refreshed his/her browser), and the task never got started again.

thanks

like image 966
ShaneKm Avatar asked Mar 04 '13 13:03

ShaneKm


People also ask

How many connections SignalR can handle?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

How long do SignalR connections stay open?

The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command.

How do I send a SignalR to a specific user?

Single-user groups You can create a group for each user, and then send a message to that group when you want to reach only that user. The name of each group is the name of the user. If a user has more than one connection, each connection id is added to the user's group.


1 Answers

With SignalR 1.0, OnDisconnected should always fire, even when users close their window (the exception to this being if the server restarts).

OnDisconnected may not fire immediately, because if the client doesn't "abort" the connection it is considered "unclean" since there is a chance the client may try and reconnect (and if you reconnect you haven't disconnected according to SignalR's semantics).

By default, it should take about 30 seconds for the server to call OnDisconnected for an "unclean" disconnect. This can be reconfigured via GlobalHost.Configuration.DisconnectTimeout.

like image 163
halter73 Avatar answered Oct 11 '22 16:10

halter73