Reputation: 2225
How do I detect if the server is offline, or for some other reason cannot connect. My code looks something like this.
this.socket = io.connect(connectionInfo, {
reconnect:false
});
It does not throw any error, so a try/catch clause is not working.
Upvotes: 12
Views: 17989
Reputation: 2788
Use
this.socket.on("connect", callback)
to catch connection eventsthis.socket.on("disconnect", callback)
to catch disconnection eventsthis.socket.on("connect_failed", callback)
to catch failed connection attemptsthis.socket.io.on("connect_error", callback)
to catch if the server is offlineYou can find all events, at https://github.com/LearnBoost/socket.io/wiki/Exposed-events
Upvotes: 24
Reputation: 271
Since the 1.0 update, connection events are different. read this page for more info: http://socket.io/docs/migrating-from-0-9/
In my case, i could detect a connection error like so:
var manager = io.Manager('http://'+window.location.hostname+':3000', { /* options */ });
manager.socket('/namespace');
manager.on('connect_error', function() {
console.log("Connection error!");
});
this.socket = io.connect('http://'+window.location.hostname+':3000');
Upvotes: 4
Reputation: 1070
If your server is offline and Client tries to connect use:
socket.on('error', function (err) {
console.log(err);
});
So Client knows he can not reach the server.
Upvotes: 2