socket.io房间字典里的房间是'undefined'?

socket.io the room in the rooms dictionary is 'undefined'?

所以我从我的客户端发出一个事件,该事件发送一个字符串,其中包含我正在尝试连接的 socket/roomID。收到数据后,在服务器端由以下代码处理:

const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;

app.use(express.static(__dirname + '/public'));

//runs when the user connects to the server
function onConnection(socket){
  console.log('a user connected');
  // socket.join('some room');
  console.log('Created Room: ' + socket.id);

  socket.emit("RoomID", socket.id);
  socket.on("location", (receivedString) => {
    // socket.emit("RoomID", socket.id);
    socket.to(socket.id).emit("redirectLocation", receivedString);
    console.log('\nReceived the following data from socket "' + socket.id + '": ' + receivedString);
    console.log('The amount of people in this room is ' + io.sockets.adapter.rooms.get(socket.id).size + '.');
  });

  //runs when the user is trying to join a specific room
  socket.on("joinRoom", (roomIdentifier) => {
    console.log('The room identifier is: ' + roomIdentifier);
    console.log('The room is already in the rooms array: ' + io.sockets.adapter.rooms[String(roomIdentifier)]);
    if (io.sockets.adapter.rooms[roomIdentifier]) {
    socket.join(roomIdentifier);
    } else {
      console.log(socket.id + 'tried to join ' + roomIdentifier + 'but the room does not exist.');
    };
  });
  socket.on('disconnect', onDisconnection);
}

//runs when the user disconnects from the server
function onDisconnection(socket){
  console.log('a user disconnected');
}

//runs when the socket manager registers a connection
io.on('connection', onConnection);

//Runs when a new room was created
io.of("/").adapter.on("create-room", (room) => {
  console.log(`room ${room} was created`);
});

io.of("/").adapter.on("join-room", (room, id) => {
  console.log(`socket ${id} has joined room ${room}`);
});

http.listen(port, () => console.log('listening on port ' + port));

我不知道为什么它会以未定义的形式出现——我想可能是因为房间字典不应该有一个字符串或其他东西作为键,但我真的不确定。

分享的代码有两个问题:

检查房间是否已经存在

下面的命令显示io.sockets.adapter.rooms是一个JavaScriptMap object.

console.log(io.sockets.adapter.rooms)
// returns [object Map]

要检查地图是否包含房间名称,应使用 Map.has(key) 而不是 Map[key]:

io.sockets.adapter.rooms.has(roomIdentifier)

如上调整房间存在性检查后,代码仍然无法让用户加入房间,如下所述。

加入房间

if 语句让用户仅在房间已经存在时加入房间,否则什么也不做。因此,永远不会创建房间,因此没有用户可以加入任何房间。

if (io.sockets.adapter.rooms.has(roomIdentifier)) {
  socket.join(roomIdentifier);
} else {
  console.log(socket.id + 'tried to join ' + roomIdentifier + 'but the room does not exist.');
  // Socket.join is not executed, hence the room not created.
};

socket.join 应该在 if 语句之外执行。 if 语句可能会从代码中删除,除非需要根据它采取某些特定操作。

if (io.sockets.adapter.rooms.has(roomIdentifier)) {
  // action when room already exists
} else {
  console.log(socket.id + 'tried to join ' + roomIdentifier + 'but the room does not exist.');
  // action when room is new
};

// join the room in any case
socket.join(roomIdentifier);

尝试使用 io.sockets.adapter.rooms.get(roomId) io.sockets.adapter.rooms returns a Map Object 所以使用 io.sockets.adapter.rooms[roomId] returns 未定义.