5

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

var pusher = new Pusher('MY_ACCOUNT_STRING');
pusher.connection.bind('state_change', function(states) {
  console.log(states.current);
});

然后我刷新页面,连接到推送器,禁用我的互联网连接,等待推送器检测到断开连接,重新启用我的互联网连接,然后等待推送器检测到。这是过程中chrome控制台输出的截图(点击这里查看大图):

断开连接期间的 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 
4

1 回答 1

3

这不是正常的行为。你有没有机会在不同的机器和网络上检查这个?看起来是网络问题。

问题 1。

当我禁用 wifi 时,需要 4 秒才能注意到状​​态并将状态更改disconnectedunavailable.

当我重新启用 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
});

问题2。

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

问题 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).
于 2013-10-24T13:55:58.900 回答