在Socket.io版本>中列出所有连接到房间的客户端. 1个 [英] Listing all the clients connected to a room in Socket.io version > 1

查看:173
本文介绍了在Socket.io版本>中列出所有连接到房间的客户端. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆