Socket IO 房间:获取特定房间的客户端列表 [英] Socket IO Rooms: Get list of clients in specific room

查看:61
本文介绍了Socket IO 房间:获取特定房间的客户端列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示特定房间中的客户列表.我只想显示他们的用户名,而不是他们的套接字 ID.

I'm trying to display a list of clients in a specific room. I just want to show their username, and not their socket id.

我现在所在的位置:

socket.set('nickname', "Earl");  
socket.join('chatroom1');
console.log('User joined chat room 1);

var roster = io.sockets.clients('chatroom1');
for ( i in roster )
{
   console.log('Username: ' + roster[i]);   
}

没有运气让它列出套接字 ID 或任何东西.但是希望它返回昵称.

Haven't had any luck getting it to list Socket IDs or anything. Would like it to return the nicknames however.

推荐答案

只是一些事情.

  1. 当您拥有 socket 时,您可以设置如下属性:socket.nickname = 'Earl'; 稍后使用 save 属性,例如控制台日志:console.log(socket.nickname);

  1. when you have the socket you can then set the properties like: socket.nickname = 'Earl'; later to use the save property for example in a console log: console.log(socket.nickname);

您在以下内容中缺少结束引号 ('):

you where missing a closing quote (') in your:

console.log('用户加入聊天室1);

我不完全确定你的循环.

Im not entirely sure about your loop.

以下是修改后的代码应该对您有所帮助,另外请注意我在下面使用的循环是异步的,这可能会影响您处理数据传输的方式.

Below is the amended code should help you out a bit, also be aware the loop i am using below is asynchronous and this may effect how you handle data transfers.

socket.nickname = 'Earl';
socket.join('chatroom1');

console.log('User joined chat room 1');
    
var roster = io.sockets.clients('chatroom1');
        
roster.forEach(function(client) {
    console.log('Username: ' + client.nickname);
});

为了帮助你更多,我需要查看你的所有代码,因为这没有给我上下文.

to help you out more i would need to see all your code as this does not give me context.

这篇关于Socket IO 房间:获取特定房间的客户端列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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