Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io error handling

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).

like image 251
user1856728 Avatar asked Nov 27 '12 13:11

user1856728


People also ask

How do I test a Socket.IO connection?

You can check the socket. connected property: var socket = io. connect(); console.

How do you fix Socket.IO Cors?

The error is simple to fix. Add cors and the origin that can communicate with the server, and that's all! const io = require('socket.io')(server, {cors: {origin: "*"}}); In the code above, I added a wildcard as my origin because I would like any origin to access it.


1 Answers

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

like image 60
Chris Nolet Avatar answered Nov 07 '22 18:11

Chris Nolet