Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当连接丢失或存在连接状态更改的处理程序时,调用哪个信号器方法?_C#_Asp.net_Websocket_Signalr - Fatal编程技术网

C# 当连接丢失或存在连接状态更改的处理程序时,调用哪个信号器方法?

C# 当连接丢失或存在连接状态更改的处理程序时,调用哪个信号器方法?,c#,asp.net,websocket,signalr,C#,Asp.net,Websocket,Signalr,我正在使用SignalR开发一个.NET web应用程序,其中心类类似于下面的示例类: public class ContosoChatHub : Hub { public override Task OnConnected() { // Add your own code here. // For example: in a chat application, record the association between // t

我正在使用SignalR开发一个.NET web应用程序,其中心类类似于下面的示例类:

public class ContosoChatHub : Hub
{
    public override Task OnConnected()
    {
        // Add your own code here.
        // For example: in a chat application, record the association between
        // the current connection ID and user name, and mark the user as online.
        // After the code in this method completes, the client is informed that
        // the connection is established; for example, in a JavaScript client,
        // the start().done callback is executed.
        return base.OnConnected();
    }

    public override Task OnDisconnected()
    {
        // Add your own code here.
        // For example: in a chat application, mark the user as offline, 
        // delete the association between the current connection id and user name.
        return base.OnDisconnected();
    }

    public override Task OnReconnected()
    {
        // Add your own code here.
        // For example: in a chat application, you might have marked the
        // user as offline after a period of inactivity; in that case 
        // mark the user as online again.
        return base.OnReconnected();
    }
}
更具体地说,我的web应用程序充当连接平板电脑的中心。当我关闭平板电脑上的应用程序时,它不会立即触发OnDisconnected任务,服务器尝试重新连接客户端需要20秒或更长时间

我的问题是,我应该使用哪种方法在连接丢失时立即检测连接丢失,或者,是否有连接状态处理程序在连接丢失时触发

为了防止数据丢失,考虑到平板电脑在线,而实际上它不是,我真的需要处理断开连接事件

非常感谢您的帮助

稍后编辑: 我还在Global.asax文件中包括了以下几行

GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(6);
GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(2);

在应用程序启动方法中。如调试中所示,这些值似乎已保存,实际上将时间减少了一半,从20-30秒减少到12-14秒,但仍然没有接近2-3秒。

您可以检测到服务器与SignalR client断开连接:

$.connection.hub.disconnected(function () {
alert('Server has disconnected');
});
这是调用每个方法时的官方文档:

当调用OnConnection、OnDisconnected和OnReconnected时

每次浏览器导航到新页面时,都必须建立新连接 建立,这意味着信号器将执行OnDisconnected 方法,然后是非连接方法。信号员总是创建一个 建立新连接时的新连接ID

当存在临时连接时,将调用OnReconnected方法 信号器可以自动恢复的连接中断, 例如,当电缆暂时断开并重新连接时 在连接超时之前。调用OnDisconnected方法 当客户端断开连接且信号器无法自动 重新连接,例如当浏览器导航到新页面时。因此 已连接给定客户端的可能事件序列, 重新连接时,断开连接时;或未连接,或未断开连接。你 将看不到的OnConnected、OnDisconnected、OnReconnected序列 给定的连接

OnDisconnected方法在某些情况下不会被调用,例如 当服务器停机或应用程序域被回收时。什么时候 另一台服务器上线或应用程序域完成其回收, 一些客户端可能能够重新连接并启动OnReconnected 事件


谢谢你的回复。大约需要20-30秒。我做了一些研究,似乎这次游戏是因为信号员试图重新连接,直到最后触发OnDisconnected任务。OnDisconnected和ONDISCONNECTASSYNC之间有什么区别?事实是这个函数很旧,现在我已经检查过了,我认为它不推荐使用,我编辑了我的答案。失去联系是什么意思?首先感谢你的洞察力。我还编辑了我最初的帖子。希望对我的问题有一个更清晰的看法。