Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a callback for the io.connect() method in Socket.IO?

Tags:

socket.io

Is there any callback for the io.connect() method on the client side? I would like to print some text about connection failure, otherwise proceed normally with the site's socket interactions.

like image 651
birkof Avatar asked Jun 15 '12 11:06

birkof


People also ask

Does Socket.IO auto reconnect?

In the first case, the Socket will automatically try to reconnect, after a given delay.

How does Socket.IO connection work?

Socket.IO allows bi-directional communication between client and server. Bi-directional communications are enabled when a client has Socket.IO in the browser, and a server has also integrated the Socket.IO package. While data can be sent in a number of forms, JSON is the simplest.

How do I reconnect a Socket.IO client?

socket = io. connect( 'http://127.0.0.1:3000', { reconnection: true, reconnectionDelay: 1000, reconnectionDelayMax : 5000, reconnectionAttempts: 99999 } ); socket. on( 'connect', function () { console. log( 'connected to server' ); } ); socket.

Does Socket.IO reconnect after disconnect?

Socket disconnects automatically, reconnects, and disconnects again and form a loop. #918.

How do you check Socket is connected or not?

If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.


2 Answers

Sure, checkout the documentation for Socket.IO-client with the examples there:

https://github.com/LearnBoost/socket.io-client#sockets-for-the-rest-of-us

socket.on('connect', function () {
  // socket connected
});
like image 128
alessioalex Avatar answered Oct 11 '22 06:10

alessioalex


In the current release of socket.io (1.3.x) you can use the connect_error event or the reconnect_failed event:

var socket = io(serverUrl);
socket.on('connect_error', function() {
    console.log('Connection failed');
});
socket.on('reconnect_failed', function() {
    console.log('Reconnection failed');
});

See: https://github.com/Automattic/socket.io-client#events

like image 29
Dynalon Avatar answered Oct 11 '22 07:10

Dynalon