在Socket.io版本>中列出所有连接到房间的客户端. 1个 [英] Listing all the clients connected to a room in Socket.io version > 1
问题描述
在io.sockets.clients()方法从更高版本的Socket.io中贬值之后,并且在我的研究中找不到在socket.io官方网站上的任何文档之后.
After the io.sockets.clients() method has been depreciated from the later versions of Socket.io, and after my research couldn't find any documentation on the socket.io offical web.
此外,它给出了client()方法的类型错误,如下所示:
Morever, it gives the type error for clients() method as below:
TypeError:未定义不是函数
有人知道如何使用更高版本的Socket.io列出一个房间中的所有已连接客户端吗?
Has anyone figured out how to list all the connected clients in a room with the later versions of Socket.io?
推荐答案
要获取连接到房间的客户端的套接字ID,请使用以下代码:
To get socket IDs of the clients connected to a room use this code:
var namespace = '/';
var roomName = 'my_room_name';
for (var socketId in io.nsps[namespace].adapter.rooms[roomName]) {
console.log(socketId);
}
要通过套接字ID获取套接字,您可以尝试以下操作:
To get socket by socket ID you may try this:
var socket = io.sockets.connected[socketId];
这篇关于在Socket.io版本>中列出所有连接到房间的客户端. 1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!