使用带有JS长轮询客户端的SignalR持久连接,我们可以在不同的场景中看到不一致的重新连接行为。当客户端计算机的网络电缆断开时,JS连接不会进入重新连接状态,而且它永远不会(至少在5分钟后)到达断开状态。对于其他情况,例如重新启动IIS web应用程序,长轮询JS连接确实进入重新连接状态并成功地重新连接。我理解这背后的理由是,长时间的投票传输不支持保持活力。
我可以看到有人建议github更好地支持长轮询传输(https://github.com/SignalR/SignalR/issues/1781)的重新连接,但似乎并没有改变它的承诺。
首先,在长轮询的情况下,是否有适当的方法来检测客户端上的断开连接。第二,是否有人知道是否有计划支持在所描述的情况下重新连接?
干杯
发布于 2013-10-29 18:23:32
我们讨论了支持长轮询的“保持生命”特性的不同替代方案;然而,由于轮询的工作时间很长,所以在不影响绝大多数用户的情况下很难实现。当我们继续讨论“正确”解决方案时,我将留给您一项工作来检测长轮询客户端中的网络故障(如果绝对需要的话)。
创建一个服务器方法,让我们称之为ping:
public class MyHub : Hub
{
public void Ping()
{
}
}现在,在客户机上创建一个"ping“服务器的间隔:
var proxy = $.connection.myHub,
intervalHandle;
...
$.connection.hub.disconnected(function() {
clearInterval(intervalHandle);
});
...
$.connection.hub.start().done(function() {
// Only when long polling
if($.connection.hub.transport.name === "longPolling") {
// Ping every 10s
intervalHandle = setInterval(function() {
// Ensure we're connected (don't want to be pinging in any other state).
if($.connection.hub.state === $.signalR.connectionState.connected) {
proxy.server.ping().fail(function() {
// Failed to ping the server, we could either try one more time to ensure we can't reach the server
// or we could fail right here.
TryAndRestartConnection(); // Your method
});
}
}, 10000);
}
});希望这能有所帮助!
https://stackoverflow.com/questions/19655141
复制相似问题