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.
Use
this.socket.on("connect", callback)
to catch connection events this.socket.on("disconnect", callback)
to catch disconnection events this.socket.on("connect_failed", callback)
to catch failed connection attempts this.socket.io.on("connect_error", callback)
to catch if the server is offline You can find all events, at https://github.com/LearnBoost/socket.io/wiki/Exposed-events
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');
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.