Permalink
Browse files

fixed some issues with broadcasting the list of users currently in th…

…e chat session
  • Loading branch information...
1 parent 59cb744 commit 352ed8d31bb29d141e4f7fcb075e0d1c910aac25 @Aaronontheweb Aaronontheweb committed Apr 14, 2012
Showing with 36 additions and 15 deletions.
  1. +22 −0 lib/helpers/mapper.js
  2. +9 −15 lib/server.js
  3. +5 −0 lib/views/index.jade
View
22 lib/helpers/mapper.js
@@ -0,0 +1,22 @@
+/*
+ * Maps low-level objects into consumable forms expected
+ * by our ViewModels
+ */
+
+exports.createChatMessage = function(username, data){
+ return {"author": username, "text":data, "timestamp": new Date()};
+}
+
+exports.createUser = function(username){
+ return {"username": username};
+}
+
+exports.mapArray = function(data, mapFn){
+ var arr = Object.keys(data).reduce(function(arr, id){
+ arr.push(mapFn(data[id]));
+ return arr;
+ }, []);
+
+ return arr;
+}
+
View
24 lib/server.js
@@ -5,6 +5,7 @@
var express = require('express')
, sessions = require('cookie-sessions')
+ , mapper = require('./helpers/mapper')
var app = module.exports = express.createServer();
@@ -56,35 +57,28 @@ io.configure(function () {
//Catch-all routes
//app.all('*?', initializeSession);
-var usernames = {};
-
-var createChatMessage = function(username, data){
- return {"author": username, "text":data, "timestamp": new Date()};
-}
-
-var createUser = function(username){
- return {"username": username};
-}
+var usernames = [];
/* socket-io magic */
io.sockets.on('connection', function (socket) {
socket.on('sendchat', function (data) {
- io.sockets.emit('updatechat', createChatMessage(socket.username, data));
+ io.sockets.emit('updatechat', mapper.createChatMessage(socket.username, data));
});
socket.on('newuser', function(username){
socket.username = username;
usernames[username] = username;
- socket.emit('updatechat', createChatMessage('SERVER', 'you have connected'));
- socket.broadcast.emit('updatechat', createChatMessage('SERVER', username + ' has connected'));
- io.sockets.emit('adduser', createUser(username));
+ socket.emit('updatechat', mapper.createChatMessage('SERVER', 'you have connected'));
+ socket.broadcast.emit('updatechat', mapper.createChatMessage('SERVER', username + ' has connected'));
+ socket.emit('loadusers', mapper.mapArray(usernames, mapper.createUser));
+ socket.broadcast.emit('adduser', mapper.createUser(username));
});
socket.on('disconnect', function(){
delete usernames[socket.username];
- io.sockets.emit('removeuser', createUser(socket.username));
- socket.broadcast.emit('updatechat', createChatMessage('SERVER', socket.username + ' has disconnected'));
+ io.sockets.emit('removeuser', mapper.createUser(socket.username));
+ socket.broadcast.emit('updatechat', mapper.createChatMessage('SERVER', socket.username + ' has disconnected'));
});
});
View
5 lib/views/index.jade
@@ -70,6 +70,11 @@ block append scripts
socket.emit('newuser', '#{session.userName}');
});
+ socket.on('loadusers', function(data){
+ chatModel.loadUsers(data);
+ console.log(data);
+ });
+
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on('updatechat', function (data) {
chatModel.addMessage(data);

0 comments on commit 352ed8d

Please sign in to comment.