Skip to content

Commit

Permalink
Update online users properly
Browse files Browse the repository at this point in the history
  • Loading branch information
and1zero committed Nov 19, 2016
1 parent 8683c19 commit 8a2e7de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 15 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,23 @@

var userList = document.getElementById('users');
var userTemplate = document.querySelector('#templates > .list-user');
var onlineUsers;
socket.on('load connected users', function(clients) {
userList.innerHTML = ''; // gotta reset
Object.values(clients).forEach(function(user) {
var el = userTemplate.cloneNode(true);
el.dataset.id = user;
el.querySelector('.avatar').src = 'https://robohash.org/' + user + '.png?size=48x48';
el.querySelector('p').textContent = user;
userList.appendChild(el);
onlineUsers = Object.values(clients).filter(function(user) {
return user != username;
});
userList.innerHTML = ''; // gotta reset
if (onlineUsers.length > 0) {
onlineUsers.forEach(function(user) {
var el = userTemplate.cloneNode(true);
el.dataset.id = user;
el.querySelector('.avatar').src = 'https://robohash.org/' + user + '.png?size=48x48';
el.querySelector('p').textContent = user;
userList.appendChild(el);
});
} else {
userList.innerHTML = 'You are never alone';
}
});

socket.on('chat message', appendChat);
Expand Down
5 changes: 5 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ io.on('connection', (socket) => {
socket.broadcast.emit('log', socket.username + ' has joined the chat');
if (Object.keys(clients).length == 1) {
socket.emit('log', 'Don\'t worry, you are not alone!');
} else {
// Update online user list for other users
socket.broadcast.emit('load connected users', clients);
}
});

socket.on('disconnect', () => {
socket.broadcast.emit('log', clients[socket.id] + ' has left the chat');
delete clients[socket.id];
// Update online user list for other users
socket.broadcast.emit('load connected users', clients);
});

bot.create(function(err, session) {
Expand Down

0 comments on commit 8a2e7de

Please sign in to comment.