O'Reilly logo

Multiplayer Game Development with HTML5 by Rodrigo Silveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Synchronizing the clients

As the server consistently pushes out updates about the current state of the game world, we need a way for the clients to consume and make use of this data. A simple way to achieve this is to hold the latest server state outside the game class and update itself whenever the data is available since it won't be present every update tick.

// ch4/snake-ch4/share/app.client.js // All of the requires up top // … var serverState = {}; // … socket.on(gameEvents.client_playerState, function(data){ otherPlayers = data.filter(function(_player){ if (_player.id == player.id) { serverState = _player; return false; } _player.width = BLOCK_WIDTH; _player.height = BLOCK_HEIGHT; _player.head.x = parseInt(_player.head.x / BLOCK_WIDTH, 10); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required