我们讨论不同的替代方案,以支持保活“喜欢”功能对于长轮询;然而,由于投票的覆盖时间有多长,在不影响绝大多数用户的情况下实施起来并不容易。随着我们继续辩论“正确”的解决方案,我将为您提供一项解决方案,以便在长轮询客户端中检测网络故障(如果绝对需要)。
创建一个服务器的方法,让我们称它为平:
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);
}
});
希望这有助于!
感谢您的回答。检测客户端上的断开连接对于我们的应用程序的工作是非常必要的,所以我认为我们会像您所建议的那样使用某种类型的定期ping。谢谢! – xsov