HTML and CSS Reference
In-Depth Information
The downside to processing on the server is that it means you have a lot more processor load than you would
if the clients handled all the simulation responsibilities.
The pong game in this chapter will not simulate everything on the server to keeps things simple, but because
it's JavaScript all the way down, there's no reason you couldn't have the same game code run on the clients and
authoritatively on the server.
Deploying Real-Time Apps
The hosting server used in the last chapter, Heroku, unfortunately doesn't support WebSockets because they
don't make it through Heroku's various caching and proxying layers. Because WebSockets are only slowly start-
ing to filter out into the web at large, you'll come across the same problem in some of the other managed hosting
platforms. Nodejitsu is a Node.js hosting platform that supports native WebSockets.
If you want to work around this and force the use of long polling instead of WebSockets (at a hit to perform-
ance), you can modify your server code to force long polling and limit the transports used with (a max duration
of 10 seconds is also required to prevent Heroku from timing out) the following code:
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
Your other option is to deploy Node.js on your server or VPS. Although going through the steps to do this is
outside of the scope of this topic, with Amazon micro-instances running $14 month, it's not out of reach from a
budget perspective.
Creating an Auto-Matching Server
One big part of a two-player multiplayer game is that you need two players to play the game. This provides a
matching challenge: How do you pair players?
As you may have experienced, player matching in multiplayer games can become involved, with lobbies that
show stats and images from games in progress and ones that match players up by skill level or other charac-
teristics. Instead of building a lobby that requires a front end and UI, your pong game tries to create as many
games with two players as possible. This means that the first player to hit the site will wait until the second one
arrives. If a player leaves, the next player to join will rejoin the game with one player.
The other main task of the server is simply to pass messages back and forth between the players to keep the
game in sync. In addition the server bounces a delay message from one client to the other client and back
again to try to track the timing delay between clients.
Create a new directory called pong , and start with a package.json file similar to the one from the
Scribble example in that directory, as shown in Listing 21-6 .
Listing 21-6: Pong package.json file
{
"name": "multi-player-pong"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.5.8"
 
 
Search WWH ::




Custom Search