简体   繁体   English

在Socket.IO中未正确分配房间名称

[英]Room name is not assigned correctly in Socket.IO

Upon a button being pressed client-side, a socket.emit is called with the parameter being defined by an input box. 在客户端按下按钮后,将socket.emit ,其参数由输入框定义。 Here's the server-side socket.on : 这是服务器端socket.on

socket.on('createRoom', function(roomName) {
  socket.join('Room 1');
  console.log(io.sockets.adapter);
  console.log(io.sockets.adapter.rooms);
  let clients = io.sockets.adapter.rooms['Room 1'].sockets;
  console.log('A user joined the room with the name: ' + roomName + ". The user's name is " + raceResponse.user_name);
  console.log(clients);
  socket.emit('roomCreated', clients);
});

I have since changed this to have the room name defined server-side, as I was getting an error: 从那以后,我更改了此名称,以在服务器端定义房间名称,因为出现错误:

let clients = io.sockets.adapter.rooms['Room 1'].sockets;
TypeError: Cannot read property 'sockets' of undefined

But notice the lines above it: they log io.sockets.adapter and io.sockets.adapter.rooms . 但是请注意上面的io.sockets.adapter行:它们记录了io.sockets.adapterio.sockets.adapter.rooms I believe the latter is a list of all rooms, because it gives me: 我相信后者是所有房间的清单,因为它给了我:

{ ZJ81Hw0FkCIQxg86AAAA: [], UoE9k4lTnGMUB9y6AAAB: [], 'Y-Tr2j9MxCzZokSSAAAC': [ 'Y-Tr2j9MxCzZokSSAAAC': true ] }

What this looks like to me is that the first item in the object is the default room, and the other item is the room that was just created, which has one user in. What's confusing me is that the room is not called 'Room 1', which is what I would expect it to be. 在我看来,该对象中的第一个项目是默认房间,另一个项目是刚创建的房间,其中有一个用户。让我感到困惑的是,该房间未称为“房间1”。 ”,这是我期望的。

I may have completely misinterpreted what io.sockets.adapter.rooms means, so if that is the case please let me know. 我可能完全误解了io.sockets.adapter.rooms意思,因此,请告诉我。

io.sockets.adapter gives me: io.sockets.adapter给了我:

Adapter {
  nsp: Namespace {
    name: '/',
    server: Server {
      nsps: [Object],
      _path: '/socket.io',
      _serveClient: true,
      _adapter: [Function: Adapter],
      _origins: '*:*',
      sockets: [Circular],
      eio: [Server],
      httpServer: [Server],
      engine: [Server]
    },
    sockets: [ [Socket] ],
    connected: { wKTl5kLVfhWSTSt0AAAB: [Socket] },
    fns: [],
    ids: 0,
    acks: {},
    adapter: [Circular]
  },
  rooms: {
    '-zzOKEUrvDOj_345AAAA': [],
    wKTl5kLVfhWSTSt0AAAB: [ wKTl5kLVfhWSTSt0AAAB: true ]
  },
  sids: { wKTl5kLVfhWSTSt0AAAB: { wKTl5kLVfhWSTSt0AAAB: true } },
  encoder: Encoder {}
}
{
  '-zzOKEUrvDOj_345AAAA': [],
  wKTl5kLVfhWSTSt0AAAB: [ wKTl5kLVfhWSTSt0AAAB: true ]
}

Can anyone provide any explanation or alternative method of doing this? 任何人都可以提供任何解释或替代方法吗? It would be much appreciated. 将不胜感激。

Not knowing your version of Socket-io but when the server is set up like: 不知道您的Socket-io版本,但是服务器的设置如下:

let io = require('socket.io');

io.on('connection', function(socket){
    socket.join('Test');
    socket.join('Test1');
    console.log(io.sockets.adapter.rooms);
});

And the client is set up like: 客户端设置如下:

let io = require('socket.io-client');

socket = io.connect("http://localhost:8090")

The result that I get from the server on Nodejs v8.9.3 / Socket.io v2.2.0: 我从Nodejs v8.9.3 / Socket.io v2.2.0上的服务器获得的结果是:

{
    c9kG4WD5hFlLUOZrAAAA: Room { sockets: { c9kG4WD5hFlLUOZrAAAA: true }, length: 1 },
    Test: Room { sockets: { c9kG4WD5hFlLUOZrAAAA: true }, length: 1 },
    Test2: Room { sockets: { c9kG4WD5hFlLUOZrAAAA: true }, length: 1 }
}

Digging through Socket.IO documentation I found a reference to a method to show all of the rooms a socket is in and tested that: 深入研究Socket.IO文档,我找到了对一种方法的引用,该方法显示了套接字所在的所有房间并进行了测试:

let rooms = Object.keys(io.sockets.adapter.rooms);
console.log(rooms);

Which returned: 哪个返回:

[ 'c9kG4WD5hFlLUOZrAAAA', 'Test', 'Test2' ]

Which is an array of all the currently existing rooms by name only. 仅按名称列出所有当前存在的房间。

I sort of refined that with: 我通过以下方式对此进行了改进:

let roomName = rooms[rooms.length-1]; //Get last room pushed
console.log('A user joined the room with the name: ' + roomName + ". The user's name is X");

Which returned: 哪个返回:

"A user joined the room with the name: Room 1. The user's name is X"

You could track the names of the rooms yourself in an array, but you would want to be careful as that could introduce a potential memory leak. 您可以自己以阵列形式跟踪房间的名称,但是您要小心一些,因为这可能会导致潜在的内存泄漏。

But if all you want is the very last room joined, you could just use whatever value as a var and pass that through the emit. 但是,如果您想要的只是最后一个加入的房间,则可以将任何值用作var并将其传递给emit。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM