Is there something that I can do on the client side to detect that the socket.io websocket is not available? Something along the lines of:
我能在客户端做些什么来检测socket.io websocket是不可用的吗?有点像:
- server starts as per usual
- 服务器按照惯例启动
- clients connect
- 客户连接
- messages are sent back and forth between server and client(s)
- 消息在服务器和客户端之间来回发送
- server shuts down (no longer available)
- 服务器关闭(不再可用)
- warn the connected clients that the server is not available
- 警告连接的客户端服务器不可用
I tried to add the 'error' and 'connect_failed' options on the client side but without any luck, those didn't trigger at all. Any ideas from anyone how I can achieve this?
我试图在客户端添加'error'和'connect_failed'选项,但没有任何运气,那些根本没有触发。任何人的想法我怎么能做到这一点?
4 个解决方案
#1
9
The disconnect
event is what you want to listen on.
断开连接事件是您想要听的内容。
var socket = io.connect();
socket.on('connect', function () {
alert('Socket is connected.');
});
socket.on('disconnect', function () {
alert('Socket is disconnected.');
});
#2
8
If you want to be able to detect that the client was not able to connect to the server, then try using connect_error
. This works for me with socket.io-1.3.5.js. I found this in https://*.com/a/28893421/2262092.
如果您希望能够检测到客户端无法连接到服务器,请尝试使用connect_error。这适用于socket.io-1.3.5.js。我在https://*.com/a/28893421/2262092中找到了这个。
Here's my code snippet:
这是我的代码片段:
var socket = io.connect('http://<ip>:<port>', {
reconnection: false
});
socket.on('connect_error', function() {
console.log('Failed to connect to server');
});
#3
0
hit this bug during my development and noticed my event calls were doubling up every time i reset the server, as my sockets reconnected. Turns out the solution that worked for me, which is not duping connections is this
在我开发过程中遇到这个bug,并注意到每次重置服务器时我的事件调用都会加倍,因为我的套接字重新连接。结果证明对我有用的解决方案,这不是重复连接
var socket = io.connect();
var socket = io.connect();
socket.on('connect', function () {
socket.on('connect',function(){
console.log('User connected!');
console.log('用户连接!');
});
});
socket.on('message', function(message) {
socket.on('message',function(message){
console.log(message);
的console.log(消息);
});
});
( Found this at https://github.com/socketio/socket.io/issues/430 by KasperTidemann )
(在KasperTidemann的https://github.com/socketio/socket.io/issues/430上找到)
Turns out, it was becuase I put the 'message' listener inside the 'connect' function. Seating it outside of the listener, solves this problem.
事实证明,这是因为我把'message'监听器放在'connect'函数中。将它放在听众之外,解决了这个问题。
Cheers to Kasper Tidemann, whereever you are.
欢迎来到Kasper Tidemann,无论你是谁。
Moving on!!
继续!!
#4
0
connect_error
didn't work for me (using Apache ProxyPass and returns a 503).
connect_error对我不起作用(使用Apache ProxyPass并返回503)。
If you need to detect an initial failed connection, you can do this.
如果需要检测初始连接失败,可以执行此操作。
var socket;
try {
socket = io();
}
catch(e) {
window.location = "nodeServerDown.php";
}
Redirects the user to a custom error page when the server is down.
服务器关闭时,将用户重定向到自定义错误页面。
If you need to handle a disconnect after you've connected once.
如果您需要在连接一次后处理断开连接。
You do this:
你做这个:
socket.on('disconnect', function() {
//whatever your disconnect logic is
});
#1
9
The disconnect
event is what you want to listen on.
断开连接事件是您想要听的内容。
var socket = io.connect();
socket.on('connect', function () {
alert('Socket is connected.');
});
socket.on('disconnect', function () {
alert('Socket is disconnected.');
});
#2
8
If you want to be able to detect that the client was not able to connect to the server, then try using connect_error
. This works for me with socket.io-1.3.5.js. I found this in https://*.com/a/28893421/2262092.
如果您希望能够检测到客户端无法连接到服务器,请尝试使用connect_error。这适用于socket.io-1.3.5.js。我在https://*.com/a/28893421/2262092中找到了这个。
Here's my code snippet:
这是我的代码片段:
var socket = io.connect('http://<ip>:<port>', {
reconnection: false
});
socket.on('connect_error', function() {
console.log('Failed to connect to server');
});
#3
0
hit this bug during my development and noticed my event calls were doubling up every time i reset the server, as my sockets reconnected. Turns out the solution that worked for me, which is not duping connections is this
在我开发过程中遇到这个bug,并注意到每次重置服务器时我的事件调用都会加倍,因为我的套接字重新连接。结果证明对我有用的解决方案,这不是重复连接
var socket = io.connect();
var socket = io.connect();
socket.on('connect', function () {
socket.on('connect',function(){
console.log('User connected!');
console.log('用户连接!');
});
});
socket.on('message', function(message) {
socket.on('message',function(message){
console.log(message);
的console.log(消息);
});
});
( Found this at https://github.com/socketio/socket.io/issues/430 by KasperTidemann )
(在KasperTidemann的https://github.com/socketio/socket.io/issues/430上找到)
Turns out, it was becuase I put the 'message' listener inside the 'connect' function. Seating it outside of the listener, solves this problem.
事实证明,这是因为我把'message'监听器放在'connect'函数中。将它放在听众之外,解决了这个问题。
Cheers to Kasper Tidemann, whereever you are.
欢迎来到Kasper Tidemann,无论你是谁。
Moving on!!
继续!!
#4
0
connect_error
didn't work for me (using Apache ProxyPass and returns a 503).
connect_error对我不起作用(使用Apache ProxyPass并返回503)。
If you need to detect an initial failed connection, you can do this.
如果需要检测初始连接失败,可以执行此操作。
var socket;
try {
socket = io();
}
catch(e) {
window.location = "nodeServerDown.php";
}
Redirects the user to a custom error page when the server is down.
服务器关闭时,将用户重定向到自定义错误页面。
If you need to handle a disconnect after you've connected once.
如果您需要在连接一次后处理断开连接。
You do this:
你做这个:
socket.on('disconnect', function() {
//whatever your disconnect logic is
});