Socket.io与服务器脱机连接

Ada*_*dam 9 javascript node.js socket.io

如何检测服务器是否脱机,或由于某些其他原因无法连接.我的代码看起来像这样.

this.socket = io.connect(connectionInfo, {
    reconnect:false
});
Run Code Online (Sandbox Code Playgroud)

它不会抛出任何错误,因此try/catch子句不起作用.

Kur*_*tyn 17

使用

  • this.socket.on("connect", callback) 捕获连接事件
  • this.socket.on("disconnect", callback) 抓住断线事件
  • this.socket.on("connect_failed", callback) 捕获失败的连接尝试

使用this.socket.io.on("connect_error",callback)来捕获服务器是否脱机.

您可以在https://github.com/LearnBoost/socket.io/wiki/Exposed-events找到所有活动.

  • 如果这有助于其他人发现这一点——“connect_failed”和“reconnect_failed”事件在 Socket.io 的 0.9.x 版本(包括 0.9.16)中没有正确触发。更多详细信息:https://github.com/LearnBoost/socket.io/issues/652 和解决方法:https://groups.google.com/forum/#!topic/socket_io/7BynuejEmts (2认同)