SignalR is a server-side method to determine if a client is disconnecting from a hub?

I want to stop System.Timers.Timerwhich is running in the SignalR hub after the client closes the window / tab containing the active connection.

I tried sending the bool value to the server by calling the server code to notify the server to which the client is still connected or not, but it is currently not 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 this has something to do with the anonymous delegate for timer.Elapsed, but I'm not quite sure.

+4
source share
2 answers

SignalR OnConnected, OnDisconnected OnReconnected, , . :

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

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

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

, . , connectionMapping , .

+11

OnDisconnected. , , .

+3

Source: https://habr.com/ru/post/1536172/


All Articles