Time for action – sending a message to the server through WebSockets
- First, code the server logic.
- Open
servergame.js
. Add the following function to the file that handles user messages:Room.prototype.handleOnUserMessage = function(user) { var room = this; user.socket.on("message", function(message){ console.log("Receive message from " + user.id + ": " + message); }); };
- Add the following code inside the
Room.prototype.addUser
method that calls our newly created function:this.handleOnUserMessage(user);
- Now, move on to the
client
folder. - Open the
index.html
file and add the following markup in thebody
section. This provides inputs for the user to type and send messages to the server:<input type="text" id="chat-input" autocomplete="off"> <input type="button" value="Send" id="send">
- Then, add the following code to the
html5games.websocket.js
JavaScript file. This sends the message...