Get connection status on Socket.io client

后端 未结 5 1810
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 10:50

I\'m using Socket.io, and I\'d like to know the status of connection to the server from the client-side.

Something like this:

socket.status // return         


        
5条回答
  •  渐次进展
    2020-12-23 11:33

    You can check the socket.connected property:

    var socket = io.connect();
    console.log('check 1', socket.connected);
    socket.on('connect', function() {
      console.log('check 2', socket.connected);
    });
    

    It's updated dynamically, if the connection is lost it'll be set to false until the client picks up the connection again. So easy to check for with setInterval or something like that.

    Another solution would be to catch disconnect events and track the status yourself.

提交回复
热议问题