Sparcade: Edison as a Web Server for Browser Games

Pages
Contributors: Shawn Hymel
Favorited Favorite 2

The Code

Download the Sparcade game server code:

Unzip it and open the project in XDK. Connect to the Edison, upload the code, and run it. If you need a refresher on how to use the XDK, see this guide.

XDK  running Sparcade code

The XDK showing the Sparcade server running. Click on the image to see a larger version.

What's Going On?

The code for Sparcade might be confusing at first glance. In main.js, we use the express and http node modules to create a simple web server. These serve the files found in the www directory (our game).

If you open up www/index.html, you will see the basic web page that gets sent to any client that requests a page from "sparcade.local". This page tells the client's browser to download a few JavaScript (.js) files, which then run a Phaser-based game on the Canvas element in the page.

Whenever the player dies (loses all lives), the client creates a socket.io connection back to the server and sends the player's score. The server compares the player's score against its list of Top 10 high scores, and, if the player ranked, it sends an initials request back to the client.

The player, if ranked in the top scores, enters their initials into the game, and their client sends their initials back to the server. The server then adds the user's initials and score to the list of Top 10 scores, which is continuously displayed on the LCD.