diff --git a/examples/chat/index.js b/examples/chat/index.js index cc91fa2eb6..d9b4a6b8b5 100644 --- a/examples/chat/index.js +++ b/examples/chat/index.js @@ -43,7 +43,8 @@ io.on('connection', function (socket) { }); // echo globally (all clients) that a person has connected socket.broadcast.emit('user joined', { - username: socket.username + username: socket.username, + numUsers: numUsers }); }); @@ -70,7 +71,8 @@ io.on('connection', function (socket) { // echo globally that this client has left socket.broadcast.emit('user left', { - username: socket.username + username: socket.username, + numUsers: numUsers }); } }); diff --git a/examples/chat/public/main.js b/examples/chat/public/main.js index fe385b10ab..b715b85aaf 100644 --- a/examples/chat/public/main.js +++ b/examples/chat/public/main.js @@ -25,6 +25,16 @@ $(function() { var socket = io(); + function addParticipantsMessage (data) { + var message = ''; + if (data.numUsers === 1) { + message += "there's 1 participant"; + } else { + message += "there're " + data.numUsers + " participants"; + } + log(message); + } + // Sets the client's username function setUsername () { username = cleanInput($usernameInput.val().trim()); @@ -219,14 +229,10 @@ $(function() { connected = true; // Display the welcome message var message = "Welcome to Socket.IO Chat — "; - if (data.numUsers === 1) { - message += "there's 1 participant"; - } else { - message += "there're " + data.numUsers + " participants"; - } log(message, { prepend: true }); + addParticipantsMessage(data); }); // Whenever the server emits 'new message', update the chat body @@ -237,11 +243,13 @@ $(function() { // Whenever the server emits 'user joined', log it in the chat body socket.on('user joined', function (data) { log(data.username + ' joined'); + addParticipantsMessage(data); }); // Whenever the server emits 'user left', log it in the chat body socket.on('user left', function (data) { log(data.username + ' left'); + addParticipantsMessage(data); removeChatTyping(data); });