我正在尝试实现一个功能,通知用户断开连接到推动器,并指示何时发生重新连接.我的第一个实验只是将更改推送状态记录到控制台:
var pusher = new Pusher('MY_ACCOUNT_STRING');
pusher.connection.bind('state_change', function(states) {
console.log(states.current);
});
Run Code Online (Sandbox Code Playgroud)
然后我刷新页面,连接到推送器,禁用我的互联网连接,等待推送器检测断开连接,重新启用我的互联网连接,并等待推送器检测到.这是在此过程中chrome控制台输出的屏幕截图(单击此处查看大图):

这是我的问题:
谢谢你的帮助!
我一直在观察输出的长期连接,我也已经多次看过这个,并且想知道它的原因,以及我如何捕获它并处理它?
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
Run Code Online (Sandbox Code Playgroud)
这不是正常行为。您有机会在不同的机器和网络上检查这一点吗?看起来像是网络问题。
当我禁用 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
});
Run Code Online (Sandbox Code Playgroud)
不知道,我认为库不应该抛出这些错误
错误来自 WebSocket 协议:https://www.rfc-editor.org/rfc/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).
Run Code Online (Sandbox Code Playgroud)