Socket.io命名空间中的用户数

有没有内置的方法来找出连接到使用Socket.io的Node.js服务器中的特定命名空间的用户数量?

有更好的方法,但这应该工作:

Object.keys(io.of('/chat').manager.handshaken).length //handshaken clients Object.keys(io.of('/chat').manager.connected).length //connected clients Object.keys(io.of('/chat').manager.open).length //open clients Object.keys(io.of('/chat').manager.closed).length //closed clients 

你可以用你的名字空间replace/chat 。 命名空间必须以/开头。 这些是存储连接用户的socket.io对象(做console.log(io.of('/chat').manager.connected)来查看对象)中的对象。

更新

io.of('/chat').clients().length 。 请参阅io.of('/chat').clients()获取所有连接的用户详细信息。