如何将 parameters/settings 添加到我的插座室?

How do I add parameters/settings to my socket room?

我正在尝试将 parameters/settings 添加到默认房间 (roomid) 顶部的我的插座房间 我尝试阅读 socket.io 文档并在网上查找,但找不到太多帮助

我初始化一个新的游戏房间id并使用下面的代码片段加入房间

let newRoom = `A ${colors[rando[0]]} ${animals[rando[1]]}`
socket.join(newRoom)

我试过了:

io.sockets.adapter.rooms[newRoom].roomSetting = "foo"

我正在使用 socket.io v4!

我最终初始化了我自己的房间对象,每个房间都有附加的设置。花了太多时间试图找出插座房间,但我想事情 un-documented 是有原因的。吸取教训!

const rooms = {};

初始化房间后删除任何 dead/destroyed 个房间,

function getActiveRooms(rooms) {
    const arr = Array.from(io.sockets.adapter.rooms);
    const filtered = arr.filter(room => !room[1].has(room[0]))
    const res = filtered.filter(i => i[1].size === 1);
    const r = res.map(r => r[0])

    // loop through rooms and remove rooms that are not in r
    // and store someSettings value in an array
    const someSetting = [];
    for (const [key, value] of Object.entries(rooms)) {
        if (!r.includes(key)) {
            delete rooms[key]
        } else {
            someSetting.push(value.someSetting)
        }
    }


    return ([r, someSettings]);
}

输出类似于...

["roomName", "someSetting1", "someSetting2" ...etc]