如何使用SignalR事件保持正确的方式连接活着吗? [英] How to use SignalR events to keep connection alive in the right way?

查看:3420
本文介绍了如何使用SignalR事件保持正确的方式连接活着吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用SignalR,ASP.NET和C#实时客户端 - 服务器应用程序。我使用localhost作为主机和VS2013。

我的问题是:


  1. 为什么,如果我的关闭的服务器,网络客户端上的重新连接事件发生?


  2. 断开事件后的 40+秒只发生。如何减少这个时间呢?


  3. 我需要在客户端连接到服务器上的开始。在重新连接事件发生时应该在固定间隔的唯一。如果重新连接间隔时间超过客户端应连接为一个新的客户。如何归档这一目标呢?


最后,我想问一下 - 如何永葆连接使用 SignalR 正道

我用这code:

C#

 公众覆盖任务OnDisconnected()
{
clientList.RemoveAt(参数nIndex);
Console.WriteLine(断开连接{0} \\ n,Context.ConnectionId);
返回(base.OnDisconnected());
}公众覆盖任务OnReconnected()
{
Console.WriteLine(重新连接{0} \\ n,Context.ConnectionId);
返回(base.OnReconnected());
}

的JavaScript

  $。connection.hub.reconnected(函数(){
// EN的Html code显示名称和消息。
变种EN codedName = $('< D​​IV />')。文本(心跳)HTML();
VAR现在=新的日期();//信息添加到页面。
$('#讨论')追加。(重新连接到服务器:现在+ +'< / BR>');
});$ .connection.hub.disconnected(函数(){
// EN的Html code显示名称和消息。
变种EN codedName = $('< D​​IV />')。文本(心跳)HTML();
VAR现在=新的日期();
//信息添加到页面。
$('#讨论')追加(从服务器断开连接:现在+ +'< / BR>');。
});

在连接输出:

 从服务器收到的消息:周五2014年2月21日10时53分02秒

关闭服务器后输出:

 重新连接到服务器:周五2014年2月21日10时53分22秒<  - 为什么,如果我关闭服务器?
从服务器断开连接:周五2014年2月21日10时53分53秒< - 服务器被关闭,为什么40多秒后?


解决方案

1。之后,我的关闭的服务器,Web客户机上出现重新连接事件和断开连接事件仅发生后。为什么呢?

SignalR不能告诉关闭服务器和重新启动服务器之间的差异。出于这个原因,当服务器关闭客户端将开始尝试在情况下,服务器实际重新启动重新连接。

2。 断开出现的 30秒以上后未知重新连接的。如何减少这个时间呢?

这个30秒的超时可以通过<一个被修改href=\"http://www.asp.net/signalr/overview/signalr-20/hubs-api/handling-connection-lifetime-events#disconnecttimeout\">DisconnectTimeout物业。

3。我需要的客户端连接到在启动服务器。在重新连接应该内发生固定间隔的唯一。如果重新连接间隔时间超过客户端需要连接新的客户端。

您应该<一个href=\"http://www.asp.net/signalr/overview/signalr-20/hubs-api/handling-connection-lifetime-events#continuousreconnect\">start在超时后pferably断开连接的情况下,$ P $连接,以减少服务器的负载,如果它重新启动。

  $。connection.hub.disconnected(函数(){
    的setTimeout(函数(){
        $ .connection.hub.start();
    },5000); // 5秒后重新启动连接
});

整个<一个href=\"http://www.asp.net/signalr/overview/signalr-20/hubs-api/handling-connection-lifetime-events\">Understanding在SignalR 文章处理连接生存期活动可能是relavent你的问题。

I am developing a real-time client-server application using SignalR, ASP.NET and C#. I am using localhost as host and 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. 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?

I am using this code:

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

$.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. Why?

SignalR cannot tell the difference between closing the server and restarting the server. 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". How to reduce this time?

This 30 second timeout can be modified via the DisconnectTimeout property.

3. I need the client to connect to the server on start. 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事件保持正确的方式连接活着吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆