Time for action – sending messages to all connected browsers
Carry out the following steps:
- Open the
game.js
file in the server folder for the server-side logic. - 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); });
- That is it for the server side. Move on to the
client
folder and open theindex.html
file. - 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>
- Next, we need the client-side JavaScript to handle the received message from ...
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.