Say you're making a Counter-Strike server. We have a single lobby and multiple rooms in which game sessions occur.
I.E I have 3 node server, nginx load balancer, redis.
I'm using socket.io, with redis adapter, So I can emit messages to every clients on any server.
I can't understand one thing. For example I have something like this:
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;
}
}
I've just written this code, for example.
let, user_1 is on node-1 and user-2 is on node-2.
If user_1 creates game, instance will be created and saved on node-1.
so when user_2 receives information about game, which was created and he clicks a join button, client will send request to server and on node-2 there won't be the game instance, which user_1 created.
I hope, you will understand, what I'm talking (my English isn't good).
I guess, that currentGames
object of game instances must be a global, for all nodes. But I don't know how.
Should I use redis or mongo or something else, for storing game information, instead of variable
?
Am I going with wrong way?