getting how many people are in a chat room in sock

2019-01-04 07:36发布

I have this code right now that sets the nick and room:

io.sockets.on('connection', function(client){
    var Room = "";
    client.on("setNickAndRoom", function(nick, fn){
        client.join(nick.room);
        Room = nick.room;
        client.broadcast.to(Room).emit('count', "Connected:" + " " + count);
        fn({msg :"Connected:" + " " + count});
    });

I wanted to know how I could get how many people are connected to a specific chatroom...like Room.length

client side :

function Chat(){
    this.socket = null;
    this.Nickname = "";
    this.Room = "";
    var synched = $('#syncUp');
    this.Connect = function(nick, room){ 
        socket =  io.connect('http://vybeing.com:8080');    
        Nickname = nick;
        Room = room;
        //conectarse
        socket.on('connect',function (data) {
            socket.emit('setNickAndRoom', {nick: nick, room: room}, function(response){
                $("#connection").html("<p>" + response.msg + "</p>");
            });
        });
}

I found this, but it gives undefined:

count = io.rooms[Room].length;

8条回答
The star\"
2楼-- · 2019-01-04 08:19

For socket.io versions >= 1.0:

Note that rooms became actual types with a .length property in 1.4, so the 1.4.x method should be stable from now on.

To count all clients connected to 'my_room':

1.4+:

var room = io.sockets.adapter.rooms['my_room'];
room.length;

1.3.x:

var room = io.sockets.adapter.rooms['my_room'];
Object.keys(room).length;

1.0.x to 1.2.x:

var room = io.adapter.rooms['my_room'];
Object.keys(room).length;

This is assuming you're running with the default room adapter on a single node (as opposed to a cluster). Things are more complicated if you're in a cluster.


Other related examples:

  • Count all clients connected to server:

    var srvSockets = io.sockets.sockets;
    Object.keys(srvSockets).length;
    
  • Count all clients connected to namespace '/chat':

    var nspSockets = io.of('/chat').sockets;
    Object.keys(nspSockets).length
    
查看更多
在下西门庆
3楼-- · 2019-01-04 08:19

For socket.io v2.0.3, I ended up running a redis server and use socket.io-redis plugin. Then you can do:

io.of('/').adapter.clients(['room1', 'room2'], (err, clients) => {
  console.log(clients); // an array containing socket ids in 'room1' and/or 'room2'
});

code is from https://github.com/socketio/socket.io-redis#redisadapterclientsroomsarray-fnfunction

kevzettler pointed me to socket.io-redis


The other answer for socket.io v2.0.3 from The Lazy Coder didn't work for me, it gave me a list of all connected clients, regardless of the room.

查看更多
forever°为你锁心
4楼-- · 2019-01-04 08:20

put this in a function and it will give you failsafe to prevent crashing:

var roomCount = io.nsps['/'].adapter.rooms[roomName];
if (!roomCount) return null;
return roomCount.length;
查看更多
女痞
5楼-- · 2019-01-04 08:25

In version 1.4.5

var clientNumber = io.sockets.adapter.rooms[room].length;
查看更多
Deceive 欺骗
6楼-- · 2019-01-04 08:34

If you're using version < 1,

var clients = io.sockets.clients(nick.room); // all users from room

查看更多
做个烂人
7楼-- · 2019-01-04 08:34

I'm using 1.4.6 and this did the trick:

Object.keys(io.sockets.connected).length
查看更多
登录 后发表回答