我正在使用Node-js和Socket.io创建Agar.io克隆。当点到达地图的末端时(因此x或y大于10000或小于0),它就会死亡。 因此,在我启动服务器后的第一次,它可以正常运行,但是比服务器不发出死消息。
我尝试删除套接字,因为我首先想到只有在刷新页面时才会发生(也许套接字ID不会更改),但是我发现,在第一个死亡消息之后,以后没有套接字将再次发送。
死消息(服务器端):
setInterval(function() {
socket.emit("players", players); //sending infos about all players
if(playerID === undefined || players[playerID] === undefined) { //checking if the player is joined to the match
return;
}
if(players[playerID].x < 0 || players[playerID].x > 10000 || players[playerID].y < 0 || players[playerID].y > 10000) { //checking if it should be dead
socket.emit("death"); //sending death message
players.splice(playerID, 1); //removing player from players object
}
}, 1000 / 60);
死听者(客户端):
socket.on("death", function() {
$("body").append('<div class="join-modal"><div class="container">You died! Rejoin the game:<br><input type="text" placeholder="Name"><button>Join!</button><p class="error"></p></div></div>'); //adding a rejoin modal
player.alive = false; //client side alive tracking
console.log("died");
});