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 ...

Get HTML5 Game Development by Example : Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.