socket.io获取连接的客户端数量的所有房间
我现在使用socket.io与多个节点,因此我开始使用socket.io-redis适配器( https://github.com/socketio/socket.io-redis )来帮助我在节点之间进行同步。
我想获得服务器上所有房间的列表以及每个房间中相应数量的客户端。
在任何时候,我可能有数以千计的房间,并希望快速计数每个房间。
要获得我要做的所有房间的清单:
io.of('/').adapter.allRooms((err, rooms) => { console.log(rooms); // an array containing all rooms (accross every node) });
这将返回所有房间的列表,但是现在我不确定每个用户有多less个连接。
然后,我可以运行下一个查询来分别计算每个房间的数量:
//query the first room, second room and so on... io.in('firstroom').clients((err, clients) => { console.log(clients); // an array containing socket ids in 'room3'. Aggregate them to get the count });
这似乎是非常低效的,因为我会打这么多的电话来单独计数。 有没有一个比这更有效的方法来获得所有的房间与相关的连接数清单? 我已经研究过修改适配器代码,但是我并没有真正理解代码以做出任何改变。