Book Image

HTML5 Game Development by Example: Beginner's Guide

By : Seng Hin Mak
Book Image

HTML5 Game Development by Example: Beginner's Guide

By: Seng Hin Mak

Overview of this book

Table of Contents (18 chapters)
HTML5 Game Development by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Building a Physics Car Game with Box2D and Canvas
Index

Time for action – running a WebSocket server


  1. Create a project folder for our code. Inside it, create a new directory named server.

  2. Use a terminal or the shell command prompt to change the directory into our newly created folder.

  3. Type the following command that will install a WebSocket server:

    npm install --save ws
  4. Create a new file named server.js under the server directory with the following content:

    var port = 8000;
    
    // Server code
    var WebSocketServer = require('ws').Server;
    var server = new WebSocketServer({ port: port });
    
    server.on('connection', function(socket) {
      console.log("A connection established");
    });
    
    console.log("WebSocket server is running.");
    console.log("Listening to port " + port + ".");
  5. Open the terminal and change to the server directory.

  6. Type the following command to execute the server:

    node server.js
  7. You should get the following result if this works:

    $ node server.js 
    WebSocket server is running.
    Listening to port 8000.

What just happened?

We just created a simple server logic...