8

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.

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85

2 Answers2

15

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.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • Thanks for the quick response! This is just what I needed. I will look at connection mapping to keep track of each timer. – Cameron Tinker Apr 11 '14 at 20:07
  • 1
    Sure thing. I use a slightly modified version of the `In-Memory Storage` code presented [here](http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections) and it works great – Jonesopolis Apr 11 '14 at 20:11
3

You should use the method OnDisconnected instead of the timer. Here's the official documentation, but the framework gives you events when a connected client disconnects or reconnects.

AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • 4
    OnDisconnected does not always fire though – Brent Jul 21 '14 at 17:18
  • But on all his scenarios: "The OnDisconnected method doesn't get called in some scenarios, such as when a server goes down or the App Domain gets recycled." So id DOES always fire if the Client is the reason for disconnecting. – Ole Albers May 30 '18 at 07:35