nodejs的io中的io.sockets.adapter.rooms在哪里?
Posted
技术标签:
【中文标题】nodejs的io中的io.sockets.adapter.rooms在哪里?【英文标题】:Where is io.sockets.adapter.rooms in io of nodejs? 【发布时间】:2021-02-06 05:17:07 【问题描述】:https://***.com/a/6727354/462608
简短的回答: io.sockets.adapter.rooms
我分析了io:
如该答案所示,来自 io 的套接字输出部分包含以下内容:
sockets:
manager: [Circular],
name: '',
sockets: '210837319844898486': [Object] ,
auth: false,
flags: endpoint: '', exceptions: [] ,
_events: connection: [Function] ,
adapter
在哪里? rooms
在哪里?
从io
的输出中找出适配器和房间的方法是什么?
【问题讨论】:
【参考方案1】:我认为您正在尝试在加入之前获得空间。首先,您必须加入房间,然后才能获得io.sockets.adapter.rooms
的房间您可以查看此link 了解房间
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
【讨论】:
【参考方案2】:对于当前版本的“socket.io”:“^4.1.2”,
io.sockets.adapter.rooms
是这样的地图:
地图(2) 'hgdAp3ghn1RQZk3iAAAD' => 设置(1) 'hgdAp3ghn1RQZk3iAAAD' , '测试' => 设置(1) 'hgdAp3ghn1RQZk3iAAAD'
当房间已经存在时,在本例中为“测试”。
如果您在创建房间之前调用它,那么它将是:
地图(1) 'w2e2Vnav-zmf6pm4AAAD' => 设置(1) 'w2e2Vnav-zmf6pm4AAAD'
因此,长答案是它取决于您使用的版本,对于 4.x 版本,房间只会在用户加入房间后才成为地图的一部分,而不是之前。
【讨论】:
【参考方案3】:我不确定那个回复是怎么回事。但我可以确认,当我使用 socket.io@2.1.1 和 console.log(io) 运行基本的 socket.io 示例 (codesandbox link) 时,我在终端中看到以下内容:
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
的房间。
【讨论】:
以上是关于nodejs的io中的io.sockets.adapter.rooms在哪里?的主要内容,如果未能解决你的问题,请参考以下文章
AWS - 亚马逊设备农场与 iOS 设备 NodeJS 中的 Appium 测试的兼容性
如何使用 mysql 数据库中的 nodejs 和 socket.io 在网页上获得实时更新?
如何将用户名从php会话存储到nodejs中的socket.io库和页面刷新时套接字连接断开。为啥?
如何使用nodejs socket.io向特定房间中的用户发送打字警报和消息