android signalr 自动重连,asp.net-SignalR长轮询重新连接行为

我们已经讨论了不同的替代方案,以支持长时间轮询的“赞”功能.但是,由于调查在后台进行了多长时间,因此在不影响绝大多数用户的情况下实施起来并不容易.在我们继续争论“正确”解决方案的过程中,我将为您解决一个问题,以便在长轮询客户端中检测网络故障(如果绝对需要).

创建一个服务器方法,将其称为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);

}

});

希望这可以帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值