简体   繁体   English

socket.io-检查一个人在房间里待了多长时间

[英]socket.io - check how long a person has been in a room

background 背景

So I'm trying to create a queuing system for my game. 因此,我正在尝试为我的游戏创建一个排队系统。 I want someone to who has waited for a minute to just be assigned against an AI. 我想要一个等待一分钟的人被分配给AI。 I'd also like people who have waited longer to have priority in matching opponents who are of similar skill level. 我还希望那些等待时间更长的人在匹配具有相似技能水平的对手时拥有优先权。 I was wondering the best way to go about this. 我在想最好的方法。 Should I set a setTimeout for the AI assignment and a UTC time to check how long they've been waiting? 我应该为AI分配设置setTimeout还是设置UTC时间来检查他们等待了多长时间? I'm just wondering how I would cancel the timeout afterwards if they are matched. 我只是想知道如果匹配的话如何取消超时。

timeout: 暂停:

socket.on('waiting room', function (id) {
    console.log("socket has joined the waiting room", id);
    socket.join("waiting room")
    setTimeout(function() {
        socket.leave("waiting room")
        socket.join("some ai game")
    }, 60000)
})

I suppose gameserver resource is limited in game, then players must wait. 我想游戏服务器资源有限,那么玩家必须等待。 So when a gameserver is available, we put the player who waited longest to play. 因此,当有游戏服务器可用时,我们将等待时间最长的玩家放到游戏机上。 Then the logic would look like something below. 然后逻辑看起来像下面的东西。

 const waitinglist = [] //user leave waiting const leaveWaiting = (socketid) => { waitinglist = waitinglist.filter((x) => x.socket.id != socketid) } io.on('connection', (socket) => { socket.on('waiting room', function(id) { console.log("socket has joined the waiting room", id); socket.join("waiting room") waitinglist.push({ socket, time: new Date(), }) }) //leave by themselves socket.on('disconnect', () => { leaveWaiting(socket.id) }) socket.on('cancle waiting', () => { leaveWaiting(socket.id) socket.leave("waiting room") }) }) //leave by other logic, like match/ai server available matchResource.on('available', (resource) => { const { socket, time } = waitinglist.shift() socket.leave("waiting room") start_your_game(socket, resource) }) 

a global waitinglist, you can replace it with a module something like waiting-manager 全局waiting-manager ,您可以将其替换为诸如waiting-manager类的模块

when player enter waiting room , enqueue him 当玩家进入waiting room ,将他排入队列

when gameserver is available, get a player from queue top, and let him play 如果有可用的游戏服务器,请从队列顶部吸引玩家,然后让他玩

whether player quit or begin play, remove him from the queue 无论玩家退出还是开始游戏,都可以将其从队列中删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM