Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - Server side method to detect if a client disconnects from a hub?

I'm wanting to stop a System.Timers.Timer that is running in a SignalR hub after a client closes a window/tab containing the active connection.

I have tried sending a bool value to the server by calling server code to notify the server the client is still connected or not, but it's not currently working.

window.onbeforeunload = function () {
    profile.server.setIsConnected(false);
};

Server Side:

public ProfileHub()
{  
    timer = new Timer(15000);
    timer.Elapsed += (sender, e) => { timer_Elapsed(sender, e, _isActive); };
    timer.AutoReset = false;
}

[Authorize]
private void timer_Elapsed(object sender, ElapsedEventArgs e, bool active)
{            
    timer.Stop();

    if (active)
    {
        System.Diagnostics.Debug.WriteLine("Timer Started");
        timer.Start();
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("Timer Stopped");
        return;
    }
    // process code
}

[Authorize]
public void SetIsActive(bool isActive)
{
    _isActive = isActive;
}

Is this possible and am I on the right track? I suspect it has something to do with the anonymous delegate for timer.Elapsed, but I'm not entirely sure.

like image 761
Cameron Tinker Avatar asked Apr 11 '14 19:04

Cameron Tinker


People also ask

What is the SignalR hubs API?

The SignalR Hubs API enables you to make remote procedure calls (RPCs) from a server to connected clients and from clients to the server. In server code, you define methods that can be called by clients, and you call methods that run on the client.

How does SignalR work with client code?

In client code, you define methods that can be called from the server, and you call methods that run on the server. SignalR takes care of all of the client-to-server plumbing for you.

What happens when client calls stop method in SignalR?

If the client calls the Stop method, a stop message is sent to the server, and both client and server end the SignalR connection immediately. After connectivity between client and server is lost, the client tries to reconnect and the server waits for the client to reconnect.

What happens when a SignalR connection is lost?

After connectivity between client and server is lost, the client tries to reconnect and the server waits for the client to reconnect. If the attempts to reconnect are unsuccessful and the disconnect timeout period ends, both client and server end the SignalR connection.


1 Answers

SignalR has OnConnected, OnDisconnected, and OnReconnected that are called every time the client does one of those actions. You can simply override them:

public override Task OnConnected()
{
    return base.OnConnected();
}

public override Task OnDisconnected()
{
    //custom logic here
    return base.OnDisconnected();
}

public override Task OnReconnected()
{
    return base.OnReconnected();
}

I've found them to be extremely useful also for debugging purposes. If you're wanting to set a timer for each person, you should use some sort of connectionMapping along with the above functions to keep track of your users.

like image 73
Jonesopolis Avatar answered Sep 19 '22 06:09

Jonesopolis