Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/chat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});

Expand All @@ -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
});
}
});
Expand Down
18 changes: 13 additions & 5 deletions examples/chat/public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand All @@ -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);
});

Expand Down