最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Socket.io error handling - Stack Overflow

programmeradmin0浏览0评论

I am having a problem with 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).

I am having a problem with 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).

Share Improve this question edited Sep 29, 2013 at 1:40 Chris Nolet 9,0638 gold badges68 silver badges95 bronze badges asked Nov 27, 2012 at 13:50 user1856728user1856728 1971 gold badge1 silver badge8 bronze badges 6
  • 4 1) What is the error event? 2) How do you know that it is not creating new connection? 3) Obviously you have to set all event handlers one more time when you are creating new connection, are you doing that? – freakish Commented Nov 27, 2012 at 13:59
  • 1 1) "error" is default socket.io event (github.com/LearnBoost/socket.io/wiki/Authorizing) 2)new connection don't logged 3) yes, I do – user1856728 Commented Nov 27, 2012 at 14:10
  • Logged were? On server side? Or client side? – freakish Commented Nov 27, 2012 at 14:13
  • both server and client side – user1856728 Commented Nov 27, 2012 at 14:14
  • 2 You have to show us more code of both client and server. – freakish Commented Nov 27, 2012 at 14:16
 |  Show 1 more comment

1 Answer 1

Reset to default 14

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

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

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

发布评论

评论列表(0)

  1. 暂无评论