我正在尝试实现一个功能,通知用户断开连接到推动器,并指示何时发生重新连接。我的第一个实验只是将更改推送状态记录到控制台:
var pusher = new Pusher('MY_ACCOUNT_STRING');
pusher.connection.bind('state_change', function(states) {
console.log(states.current);
});
然后我刷新页面,连接到推送器,禁用我的互联网连接,等待推送器检测到断开连接,重新启用我的互联网连接,并等待推送器检测到。以下是此过程中Chrome控制台输出的屏幕截图(click here for a larger version):
以下是我的问题:
感谢您的帮助!
我一直在观看输出的长期连接,我也已经多次看过这个,并且想知道它的原因,以及我如何捕获并处理它? / p>
disconnected login.js:146
connecting login.js:146
Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":1007,"message":"Server heartbeat missed"}}} pusher.min.js:12
connected
答案 0 :(得分:2)
这不是正常行为。你有机会在不同的机器和网络上检查这个吗?它看起来像一个网络问题。
当我禁用wifi时,需要4秒才能注意到并将状态更改为disconnected
,然后再更改为unavailable
。
当我重新启用wifi时,我只会遇到与http://js.pusher.com/2.1.3/sockjs.js
我不知道这样做的含义..但你可以尝试改变默认超时:
var pusher = new Pusher('MY_ACCOUNT_STRING', {
pong_timeout: 6000, //default = 30000
unavailable_timeout: 2000 //default = 10000
});
不知道,我认为lib不应该抛出那些错误
错误来自WebSocket协议:http://tools.ietf.org/html/rfc6455
1006 is a reserved value and MUST NOT be set as a status code in a
Close control frame by an endpoint. It is designated for use in
applications expecting a status code to indicate that the
connection was closed abnormally, e.g., without sending or
receiving a Close control frame.
1007 indicates that an endpoint is terminating the connection
because it has received data within a message that was not
consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
data within a text message).