首页 > 解决方案 > socketio 无法创建空间

问题描述

我正在尝试动态创建一个房间。我得到一个 id,这将是房间的名称。在回调中,我看到正在使用的房间列表,io.sockets.adapter.rooms但没有看到我加入的房间。

io.of('/chat').on('connection', (socket) => {
 const socketQuery = socket.handshake.query;
 if(socketQuery.action === 'createRoom' && socketQuery.doctorUserId){

   console.log("Creating room", socketQuery.doctorUserId);
   socket.join("A ROOM", () => console.log(io.sockets.adapter.rooms)); //I want the id here to be the name of the room
}

我试图只给出一些文本(A ROOM),但仍然没有在回调中看到房间名称。

我在 console.log 中看到了这个:

Creating room 5e972498125a3d1488d0985f { 'tHlX-TYkQeVlxEB9AAAA': Room { sockets: { 'tHlX-TYkQeVlxEB9AAAA': true }, length: 1 } } Creating room 5e972498125a3d1488d0985f { pZe_38mllgVtSUfrAAAB: Room { sockets: { pZe_38mllgVtSUfrAAAB: true }, length: 1 }, '7oZdWV9mwvuhnvQTAAAC': Room { sockets: { '7oZdWV9mwvuhnvQTAAAC': true }, length: 1 } }

编辑如果我不使用命名空间而只加入它,它就可以工作。我究竟做错了什么

标签: javascriptnode.jssocket.io

解决方案


推荐阅读