Socket.io Connected User Count

2019-03-08 13:42发布

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.clients().length

This returns the number of clients connected to my server. The problem is after a few connects and disconnects of users, the number starts to stay higher than it should be.

For instance, if I connect and ask my friends to, the number goes up which is correct. But when we start to disconnect and reconnect the number does not decrease.

I am running the node.js and sockets.io server on a vmware ubuntu server.

Does anyone know why this is or has a better method for finding out how many people are connected to the server?

10条回答
女痞
2楼-- · 2019-03-08 14:18

I don't see any mention of multi core apps so I'm just gonna add that since I am using multiple cores ( clusters ) I wasn't able to get the right number of sockets consistently on the client side, so I ended up saving them to my mongo instance and it is quite consistent and accurate. With this approach I can view my socket connections in style via the browser :).

Mongoose schema :

var socketSchema = mongoose.Schema({
        socket : Number
});

Usage:

//reset to 0 when the app starts ( just in case )
SocketModel.find({ "socket" : 1 } , function(err, deadSockets ) {
    if (err){
        console.log( err );
    }
    else{
        for( var i = 0 ; i < deadSockets.length ; i++ ){
            deadSockets[i].remove();                
        }
    }
});

io.on('connection', function( socket ) {
    //I found I needed to make sure I had a socket object to get proper counts consistantly
    if( socket ){
        var socketEntry = new SocketModel({ "socket" : 1 });
        socketEntry.save( function(err ){
            if (err){
                console.log( err );
            }
            else{

            }
        });
    }
    //On Disconnect
    socket.on('disconnect', function() {
        SocketModel.findOne({ "socket" : 1} , function(err, deadSocket ) {
            if (err){
                console.log( err );
            }
            else{
                deadSocket.remove();
            }
        }); 
    });
});

How many do I have ?

SocketModel.count({ "socket" : 1 } , function(err, count ) {
    if (err){
        console.log(err);
    }
    else{
        var term = "sockets";
        if( count == 1 ) term = "socket";
        console.log("Current Load: " , count , term );
    }
}); 

NOTE* I don't like using empty query objects ( {} ) so I just used { "socket" : 1 } as a dummy instead

查看更多
甜甜的少女心
3楼-- · 2019-03-08 14:22

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);
  });
});
查看更多
霸刀☆藐视天下
4楼-- · 2019-03-08 14:22

Also take a look into:

io.sockets.manager.connected

It's a clean list of key value pairs (socket id and connection state?)

查看更多
我只想做你的唯一
5楼-- · 2019-03-08 14:23

Why use an (implicit global) variable when you could always filter the array, that is returned by calling the clients() method.

function filterNullValues (i) {
  return (i!=null);
}
io.sockets.clients().filter(filterNullValues).length;
查看更多
登录 后发表回答