HTML and CSS Reference
In-Depth Information
Communication Between the Server and Client
The client begins by handshaking with the server. As part of this process, the client and server exchange a token ID
that uniquely identifies the browser session with a game Id/player Id pair. As a result, a player can participate in two
games simultaneously on two browser sessions but can also reenter the same game in case there is a disconnect. After
the handshaking, the server sends the entire state and command histories to the client. Note that, with 100 states
generated every second, these objects are rather large. Sending the current state is not sufficient, because a new client
may need to reverse time in order to handle input from a lagging client. Even so, a potential optimization would be
to send only recent states and ensure a maximum latency on a single input. The following code describes this initial
process:
On Client (network.js):
var socket = io.connect('http://' + window.location.hostname + ':' + window.location.port);
this.socket = socket;
ntp.init(socket);
socket.emit('clientinit', {
gameid: gameId,
tokenid: token
});
On Server (app.js):
ntp.sync(socket);
socket.on('clientinit', function(data) {
setTimeout(function() {
var handler = gameManager.getGameHandler(data.gameid);
var playerId = handler.tokenPlayerMap[data.tokenid];
socket.set('playerId', playerId, function() {
socket.set('gameId', data.gameid, function() {
var game = handler.game;
var firstCommandMs = (game.tick * g.MS_PER_TICK) + g.LATENCY_MS;
gameManager.registerRemotePlayer(
data.gameid,
data.tokenid,
firstCommandMs);
console.log("Sending join command");
socket.emit('serverinit', {
startTime: game.startTime,
tick: game.tick,
stateHistory: clone(game.stateHistory),
commandHistory: clone(game.commandHistory)
});
var command = {};
command[playerId] = [g.PLAYER_JOIN];
applyServerCommand(data.gameid, command);
});
});
// Wait 3 seconds before accepting the client so the ntp
// accuracy can improve.
}, 3000);
});
 
Search WWH ::




Custom Search