假设您正在制作反恐精英服务器。我们有一个大厅和多个用于进行游戏会话的房间。
IE 我有 3 个节点服务器,nginx 负载均衡器,redis。
我正在使用带有 redis 适配器的 socket.io,所以我可以向任何服务器上的每个客户端发送消息。
我无法理解一件事。例如我有这样的事情:
var currentGames = {};
socket.on('createGame', function(gameName){
var game = new Game();
game.addPlayer(socket.id);
currentGames.push();
// ... Send to all clients, that game created and they can join
});
socket.on('joinGame', function(gameName){
currentGames[gameName].addPlayer(socket.id);
// ... Send to all players of this game, that player joined
});
socket.on('walk', function(gameName, coordinates){
currentGames[socket.id].walk(player, coordinates);
// ... Get all players positions and send to clients
});
class Game
{
gameName;
players = {};
score;
constructor(name){
this.gameName = name;
}
addPlayer(player){
players[name] = new Player(player);
}
walk(id, coordinates){
this.players[id].updatePosition(coordinates);
}
}
class player
{
id;
life = 100;
coordinates = {0,0,0};
kills;
death;
constructor(id){
this.id = id;
}
updatePosition(coords){
this.coordinates = coords;
}
}
例如,我刚刚编写了这段代码。
让,user_1 在节点 1 上,用户 2 在节点 2 上。
如果 user_1 创建游戏,实例将被创建并保存在 node-1 上。
因此,当 user_2 收到有关已创建游戏的信息并单击加入按钮时,客户端将向服务器发送请求,并且在 node-2 上不会有 user_1 创建的游戏实例。
我希望你能明白我在说什么(我的英语不好)。
我猜,currentGames
游戏实例的对象必须是全局的,对于所有节点。但我不知道怎么做。
我应该使用 redis 或 mongo 或其他东西来存储游戏信息,而不是variable
?
我走错路了吗?