了解断开/重新连接进程和错误消息

Jon*_*nah 5 pusher

我正在尝试实现一个功能,通知用户断开连接到推动器,并指示何时发生重新连接.我的第一个实验只是将更改推送状态记录到控制台:

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控制台输出的屏幕截图(单击此处查看大图):

断开连接时的镀铬控制台

这是我的问题:

  1. 在推动器检测到断开连接之前,花了一分多钟,甚至可能需要2-3分钟.有没有办法减少时间,以便推动器在10秒左右检测到断开连接?
  2. 为什么我会看到那些红色错误,它们究竟是什么意思?这是正常的吗?我认为正确的设置会处理错误,因为断开事件是推送器上下文中的"预期"异常.
  3. 什么是1006错误,为什么我看到了?

谢谢你的帮助!

编辑:

我一直在观察输出的长期连接,我也已经多次看过这个,并且想知道它的原因,以及我如何捕获它并处理它?

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)

Tie*_*eme 3

这不是正常行为。您有机会在不同的机器和网络上检查这一点吗?看起来像是网络问题。

问题1.

当我禁用 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)

问题2。

不知道,我认为库不应该抛出这些错误

问题3。

错误来自 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)