我想在套接字空间变量中存储一些信息,但出现以下错误:UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'host' of undefined
这是我的代码:
io.on('connection', (socket, next) => {
socket.on('create game', async () => {
console.log('Creating game...');
socket.username = await generateUsername();
socket.roomId = generateId();
socket.join(socket.roomId);
io.sockets.adapter.rooms[socket.roomId].host = socket.username;
io.sockets.adapter.rooms[socket.roomId].isOpen = true;
io.sockets.adapter.rooms[socket.roomId].players = [socket.username];
console.log('Game created! ID: ', socket.roomId);
});
}
如果我尝试记录socket.roomId它会返回类似rBAhx0. 当我登录时io.sockets.adapter.rooms,我得到以下信息:
Map {
'PX_o3Di9sp_xsD6oAAAB' => Set { 'PX_o3Di9sp_xsD6oAAAB' },
'rBAhx0' => Set { 'PX_o3Di9sp_xsD6oAAAB' }
}
但是,当我尝试记录 时io.sockets.adapter.rooms[socket.roomId],它返回undefined。我怎样才能解决这个问题?
HUWWW
慕田峪9158850
牛魔王的故事
相关分类