This is an example of an HTML5 multiplayer game framework. It consists of a server environment running Node.js, Express, and socket.io, with a MongoDB database, using Phaser as the game engine.
With this framework you can, for example, match two or more players into a real-time game environment across any platform that runs HTML5 game engines, such as web browsers and mobile devices.
This is a code example pulled and modified from an existing game that I'm building. It contains basic matchmaking functions for a 2-player PvP turn-based game. Although it should work out-of-the-box, it should not be considered working software.
Note: There is no interface whatsoever. Upon loading, starting, and connecting, you will simply see the blank Phaser game framework running.
- Node.js
- MongoDB
- Phaser
Clone the repository, cd to it, and update node modules:
$ npm update
Optionally change settings in index.js such as:
- Server name and port (default local.host:4004)
- Database connection string and collection names
- By default, the database is configured with two collections:
- authentications - for storing OAuth profiles
- players - for storing your players
- By default, the database is configured with two collections:
- Google Play ClientID and Client Secret
- More authentication modules such as Facebook and Twitter
- Whitelisted email addresses (e.g., your beta testers)
Create and start a MongoDB instance, e.g.:
$ mongod --dbpath /path/to/data
Start the server:
$ node index.js
- Point a browser or device to http://your.local.host:4004
- If not authenticated, you will be redirected to login.html until authentication.
- If authenticated, index.html loads and begins the connection handshake process.
- Upon completion of handshake, the client instantiates Phaser. Phaser will then execute a series of game states:
- boot - initial low-level screen formatting
- preloader - load all your graphics, sound files, spritesheets, etc.
- authenticator - insert or retrieve-and-update the player's authentication profile
- prestarter - create the player and data elements, finish the connection process, and trigger the main menu
- mainmenu - the final stage of startup, from here you can create your main menu and other game states
- Important files
- index.js - main node server app
- game.server.js - core server functions for client communication
- www/game.client.js - core client functions for server communication
- www/index.html - browser index page / game entry point
- www/lib/*.js - library code such as Phaser and jQuery
- www/src/*.js - your JavaScript game code
- Node modules
- express - web server
- express-session - session management for express (required by OAuth)
- helmet - security for express
- jade - template language for web pages
- mongodb - node.js driver for MongoDB
- mongojs - the mongo API
- mongoose - MongoDB ODM
- node-uuid - unique ID generator
- passport - OAuth authentication
- passport-google - Google API for passport
- passport-google-oauth - Google OAuth strategies for passport
- socket.io - real-time socket connections
- node-gameloop - server game loop
I made this to showcase my JavaScript and Node.js code for potential employers, and because I hope this example will help other independent or professional game developers.
Author: Jeffrey "pheryx" Milling
E-mail: pheryx@gmail.com
Homepage: http://www.jeffreymilling.com
These tutorials served as inspiration and helped me understand the Node.js environment: