简体   繁体   English

SignalR - 用于检测客户端是否与集线器断开连接的服务器端方法?

[英]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. 我想在客户端关闭包含活动连接的窗口/选项卡后停止在SignalR集线器中运行的System.Timers.Timer

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. 我已经尝试通过调用服务器代码向服务器发送bool值来通知服务器客户端是否仍然连接,但它当前不工作。

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. 我怀疑它与timer.Elapsed的匿名委托有关,但我不完全确定。

SignalR has OnConnected, OnDisconnected, and OnReconnected that are called every time the client does one of those actions. SignalR具有OnConnected,OnDisconnected和OnReconnected,每次客户端执行其中一个操作时都会调用它们。 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. 如果您想为每个人设置计时器,您应该使用某种connectionMapping以及上述功能来跟踪您的用户。

You should use the method OnDisconnected instead of the timer. 您应该使用OnDisconnected方法而不是计时器。 Here's the official documentation, but the framework gives you events when a connected client disconnects or reconnects. 这是官方文档,但是当连接的客户端断开连接或重新连接时,框架会为您提供事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从SignalR中的客户端调用Hub类之外的服务器方法 - Calling a server method that is outside of the Hub class from the client in SignalR SignalR 集线器从服务器端关闭连接 - SignalR Hub close connection from server side 是否可以从javascript客户端同步调用Signalr服务器端方法? - Is it possible to synchronously call Signalr server side method from javascript client? signalR调用服务器方法,该方法调用集线器外部的回调方法。 如何从该方法调用客户端功能? - signalR calls a server method and that method calls a callback method ,outside the hub. How can I call client function from that method? 来自客户端的集线器方法未在ASP.NET Core SignalR中调用 - Hub method from client is not invoking in ASP.NET Core SignalR 从Signalr .net客户端调用集线器方法将引发异常 - Invoking hub method from signalr .net client throws exception 非MVC项目中来自客户端的SignalR服务器端方法调用 - SignalR Server side method call from Client side in Non MVC project SignalR - 如何从服务器上调用服务器上的Hub方法 - SignalR - How can I call Hub Method on server from server 在服务器端调用SignalR集线器方法 - Call SignalR hub methods on server side SignalR解析调用集线器方法的客户端 - SignalR resolve client that called hub method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM