Socket.io多次触发回调函数

时间:2013-07-24 03:33:33

标签: node.js socket.io

我正在使用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)未找到播放器:连接ID ”其中 CONNECTION ID 是连接客户端的ID。

当服务器从客户端收到“移动播放器”消息并且无法在服务器上存储的播放器阵列中找到播放器的id时,我写了当调用onMovePlayer()函数时显示的错误消息。

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

为什么这么多次抛出这个错误? socket.emits发送的是无序的吗?我很困惑。

我知道这是令人费解的,所以我感谢任何帮助或想法。老实说,我希望打出这种情况可以帮助我弄明白,但遗憾的是它没有。

1 个答案:

答案 0 :(得分: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之前发送的消息数。试试我用来调试的代码。

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