我正在使用 Socket.io 和 Node.js,我想将用户配对并将他们发送到一个新房间。
当用户连接时,在 io.on('connection')
内,我确定他们与等待在数组中配对的用户的兼容性。
如果已加入的用户与正在等待的用户兼容,我想将他们都移至新房间。
这是我目前的做法。注意:[1029387,1983934,9243802]
是用户 ID 数组。
var pendingPlayers = {
"spelling": {
"level1":[1029387,1983934,9243802],
"level2":[]
}
}
io.on('connection', function(socket) {
// check compatibility
// move current player and other queued player to new room
});
我唯一的想法是 socket.emit('room', 'new room name');
从服务器到客户端,让排队的玩家将此 ID 发送回服务器并然后使用:
io.on('connection', function(socket) {
socket.on('room', function(room) {
socket.join(room);
});
});
但是,将房间名称发送给客户端,然后将其发送回服务器似乎非常牵强。我希望有更简单的方法。
非常感谢您的建议。
最佳答案
根据您的评论
If there are no players already in the pendingPlayers object for the respective level, the user who is joining a game is added to the pendingPlayers object.
不要移动到对象,而是为该级别创建一个只有该玩家的新房间。现在,当新玩家按照您的评论到来时
Then, the next player who requests to join the respective level will be paired with the player ID that is .push()'ed from the array corresponding that level.
让下一个玩家加入上面的房间。
关于node.js - 使用 Socket.io 将另一个用户移至房间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46126099/