从 c# 客户端以正确的方式保持连接
Keeping connection alive in the right way from c# client
我有一个自托管信号器中心和连接到它的两种类型的客户端。
Web 应用程序:我可以使用断开连接事件保持连接状态,如下所示:
$(function () {
$.connection.hub.url = "http://localhost:8080/signalr";
// Declare a proxy to reference the hub.
var priceHub = $.connection.uTHub;
$.connection.hub.start();
$.connection.hub.disconnected(function () {
setTimeout(function () {
$.connection.hub.start();
}, 2000); // Restart connection after 2 seconds.
});
});
Windows 服务:
hubConnection = new HubConnection("http://localhost:8080/signalr", "source=" + feed, useDefaultUrl: false);
priceProxy = hubConnection.CreateHubProxy("UTHub");
hubConnection.Start().Wait();
在 windows 服务中,如何处理我在网络应用程序中使用的断开连接事件(2 秒行为后重新启动连接)?
提前致谢,
当光盘客户端断开连接时执行自定义逻辑是重写 hub class 上的 ondisconnected 方法,如下所示:
public override Task OnDisconnected(bool stopCalled)
{
// your custom code here...
return base.OnDisconnected(stopCalled);
}
这是我需要的:
http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-net-client#connectionlifetime
hubConnection.Closed += () => {
connected = false;
while (!connected)
{
System.Threading.Thread.Sleep(2000);
hubConnection = new HubConnection("http://localhost:8080/signalr", "source=" + feed, useDefaultUrl: false);
priceProxy = hubConnection.CreateHubProxy("UTHub");
hubConnection.Start().Wait();
connected = true;
}
};
我有一个自托管信号器中心和连接到它的两种类型的客户端。
Web 应用程序:我可以使用断开连接事件保持连接状态,如下所示:
$(function () { $.connection.hub.url = "http://localhost:8080/signalr"; // Declare a proxy to reference the hub. var priceHub = $.connection.uTHub; $.connection.hub.start(); $.connection.hub.disconnected(function () { setTimeout(function () { $.connection.hub.start(); }, 2000); // Restart connection after 2 seconds. }); });
Windows 服务:
hubConnection = new HubConnection("http://localhost:8080/signalr", "source=" + feed, useDefaultUrl: false); priceProxy = hubConnection.CreateHubProxy("UTHub"); hubConnection.Start().Wait();
在 windows 服务中,如何处理我在网络应用程序中使用的断开连接事件(2 秒行为后重新启动连接)?
提前致谢,
当光盘客户端断开连接时执行自定义逻辑是重写 hub class 上的 ondisconnected 方法,如下所示:
public override Task OnDisconnected(bool stopCalled)
{
// your custom code here...
return base.OnDisconnected(stopCalled);
}
这是我需要的:
http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-net-client#connectionlifetime
hubConnection.Closed += () => {
connected = false;
while (!connected)
{
System.Threading.Thread.Sleep(2000);
hubConnection = new HubConnection("http://localhost:8080/signalr", "source=" + feed, useDefaultUrl: false);
priceProxy = hubConnection.CreateHubProxy("UTHub");
hubConnection.Start().Wait();
connected = true;
}
};