3 回答

TA貢獻1810條經驗 獲得超4個贊
我認為您在加入之前試圖獲得空間。首先,您必須加入房間,然后才能進入房間io.sockets.adapter.rooms
let room_id = 111
io.sockets.on("connection", function (socket) {
? ? // Everytime a client logs in, display a connected message
? ? console.log("Server-Client Connected!");
? ? socket.join("_room" + room_id);
? ? socket.on('connected', function (data) {
? ? });
? ? console.log(io.sockets.adapter.rooms);
? ? socket.on('qr_code_scan', function (room_id) {
? ? ? ? io.sockets.in("_room" + room_id).emit("qr_code_scan", true);
? ? });
});
的記錄io.sockets.adapter.rooms
{bjYiUV5YZy54VedKAAAA: Room, _room111: Room}
app.js:55
_room111:Room {sockets: {…}, length: 1}
length:1
sockets:{-isBAZIB-Sm3jArgAAAB: true}
-isBAZIB-Sm3jArgAAAB:true
__proto__:Object
__proto__:Object
-isBAZIB-Sm3jArgAAAB:Room {sockets: {…}, length: 1}
length:1
sockets:{-isBAZIB-Sm3jArgAAAB: true}
-isBAZIB-Sm3jArgAAAB:true
__proto__:Object
__proto__:Object
__proto__:Object

TA貢獻1828條經驗 獲得超3個贊
對于“socket.io”的當前版本:“^4.1.2”,
io.sockets.adapter.rooms
是這樣的地圖:
Map(2) { 'hgdAp3ghn1RQZk3iAAAD' => Set(1) { 'hgdAp3ghn1RQZk3iAAAD' }, 'test' => Set(1) { 'hgdAp3ghn1RQZk3iAAAD' } }
當房間已經存在時,在本例中為“測試”。
如果您在創建房間之前調用它,那么它將是:
Map(1) { 'w2e2Vnav-zmf6pm4AAAD' => Set(1) { 'w2e2Vnav-zmf6pm4AAAD' } }
因此,長答案是它取決于您使用的版本,對于 4.x 版,房間只會在用戶加入房間后成為地圖的一部分,而不是之前。

TA貢獻1765條經驗 獲得超5個贊
我不確定該響應是怎么回事。但我可以確認,當我使用 [email protected] 和 console.log(io) 運行基本的 socket.io 示例時,我在終端中看到以下內容:
Server {
? nsps: {
? ? '/': Namespace {
? ? ? name: '/',
? ? ? server: [Circular],
? ? ? sockets: [Object],
? ? ? connected: [Object],
? ? ? fns: [],
? ? ? ids: 0,
? ? ? rooms: [],
? ? ? flags: {},
? ? ? adapter: [Adapter],
? ? ? _events: [Object: null prototype],
? ? ? _eventsCount: 1
? ? }
? },
? parentNsps: Map {},
? _path: '/socket.io',
? _serveClient: true,
? parser: {
? ? protocol: 4,
? ? types: [
? ? ? 'CONNECT',
? ? ? 'DISCONNECT',
? ? ? 'EVENT',
? ? ? 'ACK',
? ? ? 'ERROR',
? ? ? 'BINARY_EVENT',
? ? ? 'BINARY_ACK'
? ? ],
? ? CONNECT: 0,
? ? DISCONNECT: 1,
? ? EVENT: 2,
? ? ACK: 3,
? ? ERROR: 4,
? ? BINARY_EVENT: 5,
? ? BINARY_ACK: 6,
? ? Encoder: [Function: Encoder],
? ? Decoder: [Function: Decoder]
? },
? encoder: Encoder {},
? _adapter: [Function: Adapter],
? _origins: '*:*',
? sockets: Namespace {
? ? name: '/',
? ? server: [Circular],
? ? sockets: { WFrro9MpS4d1nSouAAAA: [Socket] },
? ? connected: { WFrro9MpS4d1nSouAAAA: [Socket] },
? ? fns: [],
? ? ids: 0,
? ? rooms: [],
? ? flags: {},
? ? adapter: Adapter {
? ? ? nsp: [Circular],
? ? ? rooms: [Object],
? ? ? sids: [Object],
? ? ? encoder: Encoder {}
? ? },
? ? _events: [Object: null prototype] { connection: [Array] },
? ? _eventsCount: 1
? },
? eio: Server {
? ? clients: { WFrro9MpS4d1nSouAAAA: [Socket] },
? ? clientsCount: 1,
? ? wsEngine: 'ws',
? ? pingTimeout: 5000,
? ? pingInterval: 25000,
? ? upgradeTimeout: 10000,
? ? maxHttpBufferSize: 100000000,
? ? transports: [ 'polling', 'websocket' ],
? ? allowUpgrades: true,
? ? allowRequest: [Function: bound ],
? ? cookie: 'io',
? ? cookiePath: '/',
? ? cookieHttpOnly: true,
? ? perMessageDeflate: { threshold: 1024 },
? ? httpCompression: { threshold: 1024 },
? ? initialPacket: [ '0' ],
? ? ws: WebSocketServer {
? ? ? _events: [Object: null prototype] {},
? ? ? _eventsCount: 0,
? ? ? _maxListeners: undefined,
? ? ? options: [Object],
? ? ? [Symbol(kCapture)]: false
? ? },
? ? _events: [Object: null prototype] { connection: [Function: bound ] },
? ? _eventsCount: 1
? },
? httpServer: Server {
? ? insecureHTTPParser: undefined,
? ? _events: [Object: null prototype] {
? ? ? connection: [Function: connectionListener],
? ? ? close: [Function: bound ],
? ? ? listening: [Function: bound ],
? ? ? upgrade: [Function],
? ? ? request: [Function]
? ? },
? ? _eventsCount: 5,
? ? _maxListeners: undefined,
? ? _connections: 2,
? ? _handle: TCP {
? ? ? reading: false,
? ? ? onconnection: [Function: onconnection],
? ? ? [Symbol(owner_symbol)]: [Circular]
? ? },
? ? _usingWorkers: false,
? ? _workers: [],
? ? _unref: false,
? ? allowHalfOpen: true,
? ? pauseOnConnect: false,
? ? httpAllowHalfOpen: false,
? ? timeout: 120000,
? ? keepAliveTimeout: 5000,
? ? maxHeadersCount: null,
? ? headersTimeout: 60000,
? ? _connectionKey: '6::::8080',
? ? [Symbol(IncomingMessage)]: [Function: IncomingMessage],
? ? [Symbol(ServerResponse)]: [Function: ServerResponse],
? ? [Symbol(kCapture)]: false,
? ? [Symbol(asyncId)]: 6
? },
? engine: Server {
? ? clients: { WFrro9MpS4d1nSouAAAA: [Socket] },
? ? clientsCount: 1,
? ? wsEngine: 'ws',
? ? pingTimeout: 5000,
? ? pingInterval: 25000,
? ? upgradeTimeout: 10000,
? ? maxHttpBufferSize: 100000000,
? ? transports: [ 'polling', 'websocket' ],
? ? allowUpgrades: true,
? ? allowRequest: [Function: bound ],
? ? cookie: 'io',
? ? cookiePath: '/',
? ? cookieHttpOnly: true,
? ? perMessageDeflate: { threshold: 1024 },
? ? httpCompression: { threshold: 1024 },
? ? initialPacket: [ '0' ],
? ? ws: WebSocketServer {
? ? ? _events: [Object: null prototype] {},
? ? ? _eventsCount: 0,
? ? ? _maxListeners: undefined,
? ? ? options: [Object],
? ? ? [Symbol(kCapture)]: false
? ? },
? ? _events: [Object: null prototype] { connection: [Function: bound ] },
? ? _eventsCount: 1
? }
}?
隨著房間io.sockets.adapter.rooms。
添加回答
舉報