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.
In the first case, the Socket will automatically try to reconnect, after a given delay.
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.
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.
Socket disconnects automatically, reconnects, and disconnects again and form a loop. #918.
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.
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
});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With