最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Check in client side if a socket.io id is still connected - Stack Overflow

matteradmin9PV0评论

I would like to check in client side if a socket id is still connected.

The version of socket.io I use is <script src="/socket.io/socket.io.js"></script>.

I have tried answers in several threads, but it did not work.

Edit 1: Actually, I have 1 server which can serve many clients. In the server I have the following code which sends the socket id to a client when it is connected:

io.sockets.on('connection', function (socket) {
  console.log("LOG: just connected: " + socket.id);    
  socket.emit('id', socket.id);    
  socket.on('disconnect', function () {
    console.log("LOG: just disconnected: " + socket.id)
  })
})

Two clients may talk to each other via the server, that's why ClientA may (besides its own socket id) keep the socket id of ClientB. Thus, it will be useful for me to check (from an id) in the client-side if a client is still connected. It will be OK if this check needs to ask the server, I just want this check to be as simple and sure as possible.

I would like to check in client side if a socket id is still connected.

The version of socket.io I use is <script src="/socket.io/socket.io.js"></script>.

I have tried answers in several threads, but it did not work.

Edit 1: Actually, I have 1 server which can serve many clients. In the server I have the following code which sends the socket id to a client when it is connected:

io.sockets.on('connection', function (socket) {
  console.log("LOG: just connected: " + socket.id);    
  socket.emit('id', socket.id);    
  socket.on('disconnect', function () {
    console.log("LOG: just disconnected: " + socket.id)
  })
})

Two clients may talk to each other via the server, that's why ClientA may (besides its own socket id) keep the socket id of ClientB. Thus, it will be useful for me to check (from an id) in the client-side if a client is still connected. It will be OK if this check needs to ask the server, I just want this check to be as simple and sure as possible.

Share Improve this question edited Jun 28, 2017 at 22:14 SoftTimur asked Jun 28, 2017 at 15:43 SoftTimurSoftTimur 5,61045 gold badges166 silver badges347 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Apparently (from your ments), clientA has a socket.id of clientB and clientA wants to know if clientB is currently connected. Since a socket.id is just a string value, there is no way for clientA to tell from just the socket.id if clientB is connected or not. That information would have to e from the server.

You can have clientA ask the server if it thinks clientB is connected (by sending the socket.id of clientB to the server in a request message) and the server could respond with what it knows about clientB's connected state.

The server maintains a list of all connected clients. One of the ways you can access that list is via a map that is indexed by socket.id. So, it would be easy for the server to see if a connected client is in the map with a given socket.id.

Here's a way you could ask your server if a given socket.id is connected. This would be your server code:

io.sockets.on('connection', function (socket) {
  console.log("LOG: just connected: " + socket.id);    
  socket.emit('id', socket.id);    
  socket.on('disconnect', function () {
    console.log("LOG: just disconnected: " + socket.id)
  })

  // use the ack function argument to send a response back
  socket.on('isConnected', function(id, ackFn) {
      let otherSocket = io.sockets.connected[id];
      ackFn(!!otherSocket && otherSocket.connected);
  });
});

And, the client would send a request and listen for the response:

socket.emit('isConnected', someId, function(result) {
    console.log(someId + ": " + result ? "is connected" : "is not connected");
});

Original answer that thought you were asking how a client can tell if its own socket is connected.

A socket.io object has a .connected property that reflects what socket.io thinks is the current state of the connection.

 if (socket.connected) {
     // socket.io thinks it is still connected
 }

You can see it being used here internally in the .emit() method:

if (this.connected) {
  this.packet(packet);
} else {
  this.sendBuffer.push(packet);
}

Socket.io already implements its own heartbeat sent from server to client so it will usually notice when the connection stops working. By default, it will try to reconnect, but if that reconnection does not work immediately, the socket may remain in the disconnected state while it retries further reconnects.

I suggest sending a broadcast every 30 sec or so, This way you won't loose connection to your socket.

window.setInterval(function(){
    yoursocket.emit('broadcast');
}, 30000 /* 30 sec */);
Post a comment

comment list (0)

  1. No comments so far