Created
September 15, 2012 14:54
-
-
Save cclough/3728355 to your computer and use it in GitHub Desktop.
Server
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 app = require('express').createServer() | |
var io = require('socket.io').listen(app); | |
app.listen(8080); | |
// routing | |
app.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
// usernames which are currently connected to the chat | |
var usernames = {}; | |
io.sockets.on('connection', function (socket) { | |
// when the client emits 'sendchat', this listens and executes | |
socket.on('sendchat', function (data) { | |
// we tell the client to execute 'updatechat' with 2 parameters | |
io.sockets.emit('updatechat', socket.username, data); | |
}); | |
// when the client emits 'adduser', this listens and executes | |
socket.on('adduser', function(username){ | |
// we store the username in the socket session for this client | |
socket.username = username; | |
// add the client's username to the global list | |
usernames[username] = username; | |
// echo to client they've connected | |
socket.emit('updatechat', 'SERVER', 'you have connected'); | |
// echo globally (all clients) that a person has connected | |
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected'); | |
// update the list of users in chat, client-side | |
io.sockets.emit('updateusers', usernames); | |
}); | |
// when the user disconnects.. perform this | |
socket.on('disconnect', function(){ | |
// remove the username from global usernames list | |
delete usernames[socket.username]; | |
// update list of users in chat, client-side | |
io.sockets.emit('updateusers', usernames); | |
// echo globally that this client has left | |
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment