Created
February 6, 2018 20:22
-
-
Save nathanpeck/f2728cc77420a8e3b87ad91618418f89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numUsers = 0; | |
// when the client emits 'add user', this listens and executes | |
socket.on('add user', function (username) { | |
if (addedUser) return; | |
// we store the username in the socket session for this client | |
socket.username = username; | |
++numUsers; | |
addedUser = true; | |
socket.emit('login', { | |
numUsers: numUsers | |
}); | |
// echo globally (all clients) that a person has connected | |
socket.broadcast.emit('user joined', { | |
username: socket.username, | |
numUsers: numUsers | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment