Socketio 1.0从所有客户端获取属性

时间:2016-08-24 10:07:59

标签: javascript arrays node.js socket.io

我目前正在开发一个简单的 socketio 应用程序,其中我将一些参数发送到envents,我将它们存储到每个套接字中。

socket.on('add user', function (data) {
    if (addedUser) return;

    // we store the username in the socket session for this client.
    socket.nickname = data.nickname;
    socket.userId = data.userId;
...

然后我使用var io.engine.clients 获取所有socketio客户端,我试图在其他事件中获取该参数:

socket.on('new message', function (data) {

 var clientsRoom = io.engine.clients;

 for(var c in clientsRoom){
    console.log(" Client: " + c.userId); //Error
    console.log(" Client: " + c); //outputs the socket ID
    console.log(" Client: " + c['userId']); //Error
  }
...

但我无法获取以前为所有套接字存储的userID。 ¿我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:2)

而不是os.walk(),您应该使用io.engine.clients(它们不相同,io.sockets.socketssocket.io之上添加额外的图层。还要确保将其视为对象,而不是数组:

engine.io
相关问题