简体   繁体   English

如何使用 SignalR 事件以正确的方式保持连接活动?

[英]How to use SignalR events to keep connection alive in the right way?

I am developing a real-time client-server application using SignalR, ASP.NET and C#.我正在使用 SignalR、ASP.NET 和 C# 开发实时客户端-服务器应用程序。 I am using localhost as host and VS2013.我使用 localhost 作为主机和 VS2013。

My questions are:我的问题是:

  1. Why if I close server, on web-client the " Reconnect " event occurs?为什么如果我关闭服务器,在网络客户端上会发生“重新连接”事件?

  2. The "Disconnect" event occurs after 40+ seconds only. “断开连接”事件仅在40 秒以上发生。 How to reduce this time?如何减少这个时间?

  3. I need the client to connect to server on start.我需要客户端在启动时连接到服务器。 The "Reconnect" event should occurs within fixed interval only. “重新连接”事件应仅在固定时间间隔内发生。 If "Reconnect" interval time is over the client should connect as a new client.如果“重新连接”间隔时间结束,客户端应作为新客户端连接。 How to archive this goal?如何存档这个目标?

Finally, I would like to ask - how to keep alive connection using SignalR in the right way ?最后,我想问 - 如何以正确的方式使用SignalR保持活动连接?

I am using this code:我正在使用此代码:

C# C#

public override Task OnDisconnected()
{
clientList.RemoveAt(nIndex);
Console.WriteLine("Disconnected {0}\n", Context.ConnectionId);
return (base.OnDisconnected());
}

public override Task OnReconnected()
{
Console.WriteLine("Reconnected {0}\n", Context.ConnectionId);
return (base.OnReconnected());
}

Javascript Javascript

$.connection.hub.reconnected(function () {
// Html encode display name and message.
var encodedName = $('<div />').text("heartbeat").html();
var now = new Date();

// Add the message to the page.
$('#discussion').append('Reconnected to server: ' + now + '</br>');
});

$.connection.hub.disconnected(function () {
// Html encode display name and message.
var encodedName = $('<div />').text("heartbeat").html();
var now = new Date();
// Add the message to the page.
$('#discussion').append('Disconnected from server: ' + now + '</br>');
});

After Connect output:连接输出后:

message received from server : Fri Feb 21 2014 10:53:02

After Close Server output:关闭服务器输出后:

Reconnected to server: Fri Feb 21 2014 10:53:22 <-- Why, if i close server ???
Disconnected from server: Fri Feb 21 2014 10:53:53 <-- Why 40+ seconds after the server is closed ?

1. After I close server, on web-client the " Reconnect " event occurs and the " Disconnect " event occurs only after. 1.关闭服务器后,在 Web 客户端上,“重新连接”事件发生,而“断开连接”事件仅在此之后发生。 Why?为什么?

SignalR cannot tell the difference between closing the server and restarting the server. SignalR 无法区分关闭服务器和重新启动服务器之间的区别。 For this reason, when the server shuts down the client will start to try to reconnect in case the server is actually restarting.因此,当服务器关闭时,客户端将开始尝试重新连接,以防服务器实际重新启动。

2. The "Disconnect" occurs 30+ seconds after of unknown "Reconnect". 2. 在未知的“重新连接”之后30 秒以上发生“断开连接”。 How to reduce this time?如何减少这个时间?

This 30 second timeout can be modified via the DisconnectTimeout property .这个 30 秒超时可以通过DisconnectTimeout 属性修改。

3. I need the client to connect to the server on start. 3. 我需要客户端在启动时连接到服务器。 The "Reconnect" should occurs within fixed interval only. “重新连接”应仅在固定时间间隔内发生。 If "Reconnect" interval time is over the client should connect as new client.如果“重新连接”间隔时间结束,客户端应作为新客户端连接。

You should start the connection on the disconnected event , preferably after a timeout to reduce server load if it restarts.您应该在断开连接的事件上启动连接,最好在超时后启动连接,以减少服务器重新启动时的负载。

$.connection.hub.disconnected(function() {
    setTimeout(function() {
        $.connection.hub.start();
    }, 5000); // Re-start connection after 5 seconds
});

The entire Understanding and Handling Connection Lifetime Events in SignalR article is probably relavent to your question. SignalR文章中的整个理解和处理连接生命周期事件可能与您的问题有关。

This method is for when you do not want to change the server configure ;此方法适用于不想更改服务器配置的情况; javascript example code : javascript 示例代码:

connection.serverTimeoutInMilliseconds = 1000 * 60 * 10; // for  10 minute

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM