Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

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();
}
}


  1. To start chat_service, open a command prompt window. Navigate to the chat_service
    folder. Type the following command.
    node app.js

  2. Open a browser.

  3. Navigate to http://localhost:8080, where you should see the chat page.
    You should get a pop-up prompt for your username.

  4. Enter a name and click OK.

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

Free download pdf