简体   繁体   English

检测socket.io断开连接并销毁客户端事件

[英]detect socket.io disconnect and destroy events client side

I have html page sent by node.js server and socket.io component that connects to that server like this: 我有由node.js服务器和连接到该服务器的socket.io组件发送的html页面,如下所示:

var socket = io();

Also several socket events: 还有几个套接字事件:

socket.on('server-message', function(type, content) {
    ...
});

socket.on('server-update', function(type, content) {
    ...
});

The problem is that in the moment server is stopped, i get client side errors: 问题是在服务器停止的那一刻,我收到客户端错误:

https://example.com/socket.io/?EIO=3&transport=polling&t=LptmQyC net::ERR_CONNECTION_REFUSED

Once server is started again it crashes in a 30 seconds after. 重新启动服务器后,它会在30秒后崩溃。

It looks like i could use a detection if server is not available anymore and just destroy all socket related events, then reconnect by page refresh or some button. 如果服务器不再可用,我似乎可以使用检测,然后销毁所有与套接字相关的事件,然后通过页面刷新或某个按钮重新连接。

Maybe someone could help me with this. 也许有人可以帮助我。

I dont believe that much can be done about the error, its internal socket.io error that is a result of unsuccessful server polling. 我不认为该错误(由于服务器轮询不成功而导致的内部socket.io错误)可以做很多事情。

For better understanding, once connection is severed, socket.io goes into ajax polling and will try to reconnect as soon as possible (meaning as soon as server is up again). 为了更好地理解,一旦断开连接,socket.io会进入ajax轮询,并会尝试尽快重新连接(意味着服务器再次启动时)。

Server crash after reconnect on the other hand can be addressed very easy. 另一方面,重新连接后服务器崩溃很容易解决。 The code you presented is only functional in terms of how to connect, you are missing handlers for disconnect and optionally - reconnect. 您提供的代码仅在连接方式方面起作用,缺少用于断开连接的处理程序,还可以选择-重新连接。

I will show you how you can add to this code to manage disconnects (and prevent server crashes after bringing it back up). 我将向您展示如何添加到此代码中以管理断开连接(并防止服务器在重新启动后崩溃)。

First of all connect to server: 首先连接到服务器:

var socket = io();

Create a disconnect event right after (btw its also a code for server side disconnect event): 之后立即创建一个断开事件(同时也是服务器端断开事件的代码):

socket.on('disconnect', function() {
    destroy_socket_events();
});

Create function that will destroy all your listeners: 创建将破坏所有侦听器的函数:

var destroy_socket_events = function() {
    socket.off('disconnect');
    socket.off('server-message');
    socket.off('server-update');
    // add all other socket events that you have in here ...
};

Now what is going to happen is this: 现在将要发生的是:

  • connection is made 建立连接
  • server is stopped 服务器已停止
  • client triggers disconnect event 客户端触发断开事件
  • function destroys all of your socket listeners 函数破坏您所有的套接字侦听器
  • server is back up 服务器已备份
  • client reconnects (socket.io will do it because of the polling) 客户端重新连接(由于轮询,socket.io会这样做)
  • no listener is ever triggered at that point 那时没有触发任何听众

So you can safely reinitialize all of your code and attach listeners again properly. 因此,您可以安全地重新初始化所有代码,并再次正确附加侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM