O'Reilly logo

HTML5 Game Development by Example : Beginner's Guide - Second Edition by Makzan

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

Time for action – sending the drawing through WebSockets

Carry out the following steps:

  1. First, we need to modify the server logic. Open the game.js file and add two constants at the beginning of the file, as follows:
    // Constants
    var LINE_SEGMENT = 0;
    var CHAT_MESSAGE = 1;
  2. In the Room.prototype.addUser method, add the following code at the beginning of the method:
    this.users.push(user);
    var room = this;
    // tell others that someone joins the room
    var data = {
      dataType: CHAT_MESSAGE,
      sender: "Server",
      message: "Welcome " + user.id 
         + " joining the party. Total connection: " + this.users.length
    };  
    room.sendAll(JSON.stringify(data));
  3. We use JSON-formatted string for communicating both drawing actions and chat messages. Add the following code to the user ...

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