[英]Socket.io connect with server offline
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. 它不会抛出任何错误,因此try / catch子句不起作用。
Use 使用
this.socket.on("connect", callback)
to catch connection events this.socket.on("connect", callback)
来捕获连接事件 this.socket.on("disconnect", callback)
to catch disconnection events this.socket.on("disconnect", callback)
来捕获断开事件 this.socket.on("connect_failed", callback)
to catch failed connection attempts this.socket.on("connect_failed", callback)
捕获失败的连接尝试 this.socket.io.on("connect_error", callback)
to catch if the server is offline this.socket.io.on("connect_error", callback)
用于捕获服务器是否脱机 You can find all events, at https://github.com/LearnBoost/socket.io/wiki/Exposed-events 您可以在https://github.com/LearnBoost/socket.io/wiki/Exposed-events找到所有活动。
Since the 1.0 update, connection events are different. 自1.0更新以来,连接事件是不同的。 read this page for more info: http://socket.io/docs/migrating-from-0-9/
阅读此页面以获取更多信息: 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. 所以客户知道他无法到达服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.