Javascript Socket.io 错误处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13585712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:24:44  来源:igfitidea点击:

Socket.io error handling

javascriptnode.jssocketssocket.io

提问by user1856728

I am having a problem with Socket.IO.

我在使用 Socket.IO 时遇到问题。

I am trying to reconnect the socket after the socket errors but it won't reconnect. Here is the code:

我试图在套接字错误后重新连接套接字,但它不会重新连接。这是代码:

socket = io.connect(host, options);
socket.on('connect', this.onConnect);
socket.on('error', function() {
    //here i change options
    socket = io.connect(host, options);
});

Why it doesn't it create the new connection? (The host and port are being kept constant and it works for the first connection).

为什么它不创建新连接?(主机和端口保持不变,它适用于第一次连接)。

采纳答案by Chris Nolet

Try adding the option { 'force new connection': true }to io.connect. It sounds like it isn't retrying the connection.

尝试将选项添加{ 'force new connection': true }io.connect. 听起来它没有重试连接。

Socket IO won't reconnect to a host that it has already tried, unless you specify this option.

除非您指定此选项,否则套接字 IO 不会重新连接到它已经尝试过的主机。

Here is a snippet with the options hash specified in-line:

这是一个带有内嵌指定的选项哈希的片段:

io.connect(host, {
  'force new connection': true
});

You can learn more about the options here: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

您可以在此处了解有关选项的更多信息:https: //github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO