434 CHAPTER 10 WebSocket communications
function addUser() {
socket.emit('adduser', prompt("What's your name?"));
}
function processMessage(username, data) {
$('<b>' + username + ':</b> ' + data + '<br />')
.insertAfter($('#conversation'));
}
function updateUserList(data) {
$('#users').empty();
$.each(data, function (key, value) {
$('#users').append('<div>' + key + '</div>');
});
}
function sendMessage() {
var message = $('#data').val();
$('#data').val('');
socket.emit('sendchat', message);
$('#data').focus();
}
function processEnterPress(e) {
if (e.which == 13) {
e.preventDefault();
$(this).blur();
$('#datasend').focus().click();
}
}
- To start chat_service, open a command prompt window. Navigate to the chat_service
folder. Type the following command.
node app.js - Open a browser.
- Navigate to http://localhost:8080, where you should see the chat page.
You should get a pop-up prompt for your username. - Enter a name and click OK.
- Open another browser window and repeat the previous step.
As you type your message, you should see it displayed in both windows, as shown in
Figure 10-5.