首页 > 解决方案 > socket.io 断开连接让玩家精灵落后

问题描述

在用户断开连接时,断开连接的玩家的精灵仍然留在其他用户屏幕上。我不确定为什么。

编辑:它是固定的。当玩家加入时,我有重复的套接字事件。

服务器端:

socket.on('disconnect', function () {
      removePlayer(self, socket.id);
      delete players[socket.id];
      io.emit('disconnect', socket.id);
    });

function removePlayer(self, playerId) {
  self.players.getChildren().forEach((player) => {
    if (playerId === player.playerId) {
      player.destroy();
    }
  });
}

客户端:

this.socket.on('disconnect', function (playerId) {
    self.players.getChildren().forEach(function (player) {
      if (playerId === player.playerId) {
        player.destroy();
      }
    });
  });

标签: javascriptnode.jssocket.iomultiplayer

解决方案


推荐阅读