1

我正在使用 node.js 和 socket.io 开发一个多人画布游戏,我的节点服务器在建立客户端握手后立即抛出大量相同的错误时遇到问题。

这是来自控制台的复制/粘贴:

    info  - handshake authorized GeDlfwyEdfEIJXGvoZjG
    new player connected: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG
22:3:26 - (onMovePlayer) Player not found: GeDlfwyEdfEIJXGvoZjG

你可以在这里查看完整的代码:http: //github.com/storrdev/delta-wing

基本上我遇到了一个问题,server.js(节点脚本)的节点控制台吐出“(OnMovePlayer)Player Not Found:CONNECTION ID ”,其中CONNECTION ID是连接的客户端的ID。

当服务器从客户端接收到“移动播放器”消息并且在存储在服务器上的播放器数组中找不到播放器的 ID 时,我编写了该错误消息以在调用函数 onMovePlayer() 时显示。

我无法理解的是,在客户端向服务器发出“移动播放器”消息之前,服务器甚至不应该调用 onMovePlayer() 函数,并且客户端不应该在之后发出“移动播放器”消息建立连接,并向服务器发出“新玩家”消息。

为什么会多次抛出此错误?socket.emits 是否乱序发送?我很混乱。

我知道这很复杂,所以我很感激我能得到的任何帮助或想法。老实说,我希望将情况输入出来可以帮助我弄清楚,但不幸的是它没有。

4

1 回答 1

1

在game.js中有一些问题Game.update,它似乎在递归调用自己。

if (Game.players[0].update()) {
    //This seems to execute recursively.
    socket.emit('move player', {x: Game.players[0].getX(), y: Game.players[0].getY(), angle: Game.players[0].getAngle()});
    Game.background.update(Game.players[0].getX(), Game.players[0].getY());
}

我检查了它

count=0;
if (Game.players[0].update()) {
    socket.emit('move player', {x: Game.players[0].getX(), y: Game.players[0].getY(), angle: Game.players[0].getAngle()});
    count +=1;
    console.log(count);
    Game.background.update(Game.players[0].getX(), Game.players[0].getY());
}

我无法通过查看代码来理解逻辑并判断它是否是正确的行为。但它不断向move player服务器发送请求。

已编辑

经过一些测试,我发现问题是由于同步造成的。

当客户端加载页面时,它会立即开始发送move player,甚至在它连接到服务器并运行之前onSocketConnected。您在错误控制台上获得的行数等于控制台中Connected to socket server显示的之前发送的消息数。试试我用来调试的代码。

因此,它打破了客户端在建立连接之前不应发出“移动播放器”的假设。希望这对您有所帮助。

于 2013-07-24T05:02:50.787 回答