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

查看:96
本文介绍了套接字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.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.

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

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