我如何检测足球上的断开?

时间:2022-09-22 19:43:32

I am using socket.io in my project. I turned on reconnect feature. I want to if user disconnects from server show an alert (Your internet connection loss. Trying reconnect). And if the user reconnects again I want to show one more alert (Don't worry, you are connected).

我用插座。在我的项目io。我打开reconnect功能。如果用户从服务器断开连接显示一个警告(您的internet连接丢失)。尝试重新连接)。如果用户重新连接,我想再显示一个警告(不要担心,您已经连接)。

How can I do it?

我怎么做呢?

3 个解决方案

#1


16  

To detect on the client you use

以检测您使用的客户端

  // CLIENT CODE
  socket.on('disconnect', function(){
      // Do stuff (probably some jQuery)
  });

It's the exact same code as above for a node.js server too.

它与上面的节点代码完全相同。js服务器。

If you want for some reason to detect a user disconnecting and display it to the others, you will need to use the server one to detect it the person leaving and then emit back out a message to the others using something like:

如果出于某种原因需要检测用户断开连接并显示给其他用户,则需要使用服务器端来检测用户离开的用户,然后使用以下方法向其他用户发送消息:

socket.on('disconnect', function(){
    socket.broadcast.to(roomName).emit('user_leave', {user_name: "johnjoe123"});
});

Hope this helps

希望这有助于

#2


6  

socket.io has a disconnect event, put this inside your connect block:

套接字。io有一个断开事件,把这个放在你的连接块中:

socket.on('disconnect', function () {
    //do stuff
});

#3


1  

I handled this problem this way. I've made an emit sender on client which is calling heartbeat on server.

我是这样处理这个问题的。我在客户端创建了一个发送器,它在服务器上调用heartbeat。

socket.on('heartbeat', function() {
        // console.log('heartbeat called!');
        hbeat[socket.id] = Date.now();
        setTimeout(function() {
            var now = Date.now();
            if (now - hbeat[socket.id] > 5000) {
                console.log('this socket id will be closed ' + socket.id);
                if (addedUser) {
                    --onlineUsers;
                    removeFromLobby(socket.id);

                    try {
                        // this is the most important part
                        io.sockets.connected[socket.id].disconnect();
                    } catch (error) {
                        console.log(error)
                    }
                }
            }
            now = null;
        }, 6000);
    });

I found this code function to call:

我发现这个代码函数调用:

io.sockets.connected[socket.id].disconnect();

#1


16  

To detect on the client you use

以检测您使用的客户端

  // CLIENT CODE
  socket.on('disconnect', function(){
      // Do stuff (probably some jQuery)
  });

It's the exact same code as above for a node.js server too.

它与上面的节点代码完全相同。js服务器。

If you want for some reason to detect a user disconnecting and display it to the others, you will need to use the server one to detect it the person leaving and then emit back out a message to the others using something like:

如果出于某种原因需要检测用户断开连接并显示给其他用户,则需要使用服务器端来检测用户离开的用户,然后使用以下方法向其他用户发送消息:

socket.on('disconnect', function(){
    socket.broadcast.to(roomName).emit('user_leave', {user_name: "johnjoe123"});
});

Hope this helps

希望这有助于

#2


6  

socket.io has a disconnect event, put this inside your connect block:

套接字。io有一个断开事件,把这个放在你的连接块中:

socket.on('disconnect', function () {
    //do stuff
});

#3


1  

I handled this problem this way. I've made an emit sender on client which is calling heartbeat on server.

我是这样处理这个问题的。我在客户端创建了一个发送器,它在服务器上调用heartbeat。

socket.on('heartbeat', function() {
        // console.log('heartbeat called!');
        hbeat[socket.id] = Date.now();
        setTimeout(function() {
            var now = Date.now();
            if (now - hbeat[socket.id] > 5000) {
                console.log('this socket id will be closed ' + socket.id);
                if (addedUser) {
                    --onlineUsers;
                    removeFromLobby(socket.id);

                    try {
                        // this is the most important part
                        io.sockets.connected[socket.id].disconnect();
                    } catch (error) {
                        console.log(error)
                    }
                }
            }
            now = null;
        }, 6000);
    });

I found this code function to call:

我发现这个代码函数调用:

io.sockets.connected[socket.id].disconnect();