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 messages to all connected browsers

Carry out the following steps:

  1. Open the game.js file in the server folder for the server-side logic.
  2. Add the following highlighted code to the message event listener handler:
    user.socket.on("message", function(message){
      console.log("Receive message from " + user.id + ": " + message); 
      // send to all users in room.
      var msg = "User " + user.id + " said: " + message;
      room.sendAll(msg);
    });
  3. That is it for the server side. Move on to the client folder and open the index.html file.
  4. We want to display the chat messages in the chat history area. To do this, add the following code to the HTML file:
    <ul id="chat-history"></ul>
  5. Next, we need the client-side JavaScript to handle the received message from ...

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