HTML and CSS Reference
In-Depth Information
if (playerServerData.nextInputMs >= ms) {
// Note that although we are trying to prevent this scenario
// client-side, it can still happen because of the if
// statement above, so we have to be prepared to deal with
// it. If this happens on the server side, move the command
// up to accomodate. Note that, as above, the ms of the
// command has to be modified from the client's original
// intent.
ms = playerServerData.nextInputMs + 1;
}
playerServerData.nextInputMs = ms;
// Apply the commandlist on the server
game.addCommand(ms, playerId, commandlist);
// Broadcast the commandlist to the other clients
io.sockets.in(game.id).emit("serverinput", {
ms: ms,
commands: commandlist,
playerId: playerId
});
};
Receiving commands from the server (game.js):
addCommand: function(ms, playerId, commandList) {
this.getCommands(ms)[playerId] = commandList;
this.updateLastCommandMs(playerId, ms);
var newTick = g.intdiv(ms, g.MS_PER_TICK);
if (this.tick > newTick) {
// A change in the past has happend, we need to rewind.
this.tick = newTick;
}
}
Synchronizing Time
As part of the initial handshaking, the NTP client is initialized and given a few seconds to settle. Note that the NTP client
calculates the difference in time between the client and the server. The NTP client does not make any assumption about the
server time's correctness and shouldn't be used as a true estimate for the current world clock. With the NTP client library,
the client knows the time offset between itself and the server. This offset is important, because the server and all clients
should process the same frame of the game at roughly the same time. The NTP client prevents a client from getting too far
ahead of or behind the server or the other clients. The getNetworkTime() function in network.js returns the current time,
as agreed on by the client and server. The game engine (game.js) uses this function to throttle the game engine Throttling
means to put the processor to sleep in between frames so that the game moves at a rate that feels realistic. The following
code throttles updates so that all clients are processing the same frame at about the same moment in time:
Throttling code (in game.js):
if (g.MS_PER_TICK > 0 && this.tick > 0) {
// clockTick contains the tick that should be processed
// based on the current time. In the case of a
 
Search WWH ::




Custom Search