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

查看:20
本文介绍了在 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.

此外,它给出了clients() 方法的类型错误,如下所示:

Morever, it gives the type error for clients() method as below:

TypeError: undefined 不是函数

TypeError: undefined is not a function

有人知道如何使用更高版本的 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天全站免登陆