Socket.io Connected User Count

后端 未结 12 992
有刺的猬
有刺的猬 2021-01-30 20:24

I finally got socket.io to work properly, but I have encountered a strange problem.

I am not sure if this is the best way, but I am using:

io.sockets.cli         


        
12条回答
  •  不要未来只要你来
    2021-01-30 20:58

    I am currently using Socket.io v1.3.6 and have found that this works. It gives an accurate number when users connect and when they disconnect:

    io.sockets.sockets.length
    

    Like so:

    var io = require('socket.io').listen(server);
    io.on('connection', function(socket) {
      console.log(io.sockets.sockets.length);
      socket.on('disconnect', function() {
        console.log(io.sockets.sockets.length);
      });
    });
    

提交回复
热议问题