Merge remote-tracking branch 'refs/remotes/wlangstroth/socketio08' into socketio08

Conflicts:
	README.markdown
This commit is contained in:
Ali Asaria 2011-09-24 12:54:34 -04:00
commit 0e164fce74
7 changed files with 77 additions and 54 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@
rsync.sh rsync.sh
*.swp *.swp
*.log *.log
.monitor .monitor
node_modules/

View file

@ -40,20 +40,9 @@ how to install and run on your own computer (linux/osx)
------------------------------------------------------- -------------------------------------------------------
- install redis v2.2.2 - install redis v2.2.2
- install node.js >= 0.4.1 - install node.js >= 0.4.7
- install npm - install npm
- install these npm packages: - run `npm install`
- async
- express
- jade
- redis-client
- redis
- sanitizer
- socket.io@0.6.16 (you need to install a 0.6.x version of socket.io until the code is updated to work with the 0.7.x version)
- simplesets
- connect-redis
- connect
- (and perhaps more which you will notice when you try to start it)
- now start redis ($ redis-server) - now start redis ($ redis-server)
- now start ($ node server.js 80) where "80" is the port you want it to run on. - now start ($ node server.js 80) where "80" is the port you want it to run on.

View file

@ -5,9 +5,7 @@ var currentTheme = "bigcards";
var boardInitialized = false; var boardInitialized = false;
var socket = new io.Socket( ); var socket = io.connect();
socket.connect();
//an action has happened, send it to the //an action has happened, send it to the
//server //server
@ -20,7 +18,7 @@ function sendAction(a, d)
data: d data: d
} }
socket.send ( message ); socket.json.send ( message );
} }
socket.on('connect', function(){ socket.on('connect', function(){

View file

@ -22,10 +22,10 @@ var sid_rooms = {};
// Add a client to a room and return the sid:client mapping. // Add a client to a room and return the sid:client mapping.
exports.add_to_room = function (client, room, callback) { exports.add_to_room = function (client, room, callback) {
//console.log('Client ' + client.username + ' (' + client.sessionId + ') added to room ' + room); //console.log('Client ' + client.username + ' (' + client.id + ') added to room ' + room);
if (!(sid_rooms.hasOwnProperty(client.sessionId))) sid_rooms[client.sessionId] = new sets.Set(); if (!(sid_rooms.hasOwnProperty(client.id))) sid_rooms[client.id] = new sets.Set();
sid_rooms[client.sessionId].add(room); sid_rooms[client.id].add(room);
if (!(rooms.hasOwnProperty(room))) rooms[room] = new sets.Set(); if (!(rooms.hasOwnProperty(room))) rooms[room] = new sets.Set();
rooms[room].add(client); rooms[room].add(client);
@ -40,8 +40,8 @@ exports.add_to_room = function (client, room, callback) {
// mapping for everybody in those rooms. // mapping for everybody in those rooms.
exports.remove_from_all_rooms = function (client, callback) { exports.remove_from_all_rooms = function (client, callback) {
var affected_clients = new sets.Set(); var affected_clients = new sets.Set();
if (sid_rooms.hasOwnProperty(client.sessionId)) { if (sid_rooms.hasOwnProperty(client.id)) {
var client_rooms = sid_rooms[client.sessionId].array(); var client_rooms = sid_rooms[client.id].array();
for (var i = 0; i < client_rooms.length; i++) { for (var i = 0; i < client_rooms.length; i++) {
var room = client_rooms[i]; var room = client_rooms[i];
if (rooms.hasOwnProperty(room)) { if (rooms.hasOwnProperty(room)) {
@ -61,8 +61,8 @@ exports.remove_from_all_rooms = function (client, callback) {
} }
} }
} }
console.log('Client ' + client.username + ' (' + client.sessionId + ') disconnected.'); console.log('Client ' + client.username + ' (' + client.id + ') disconnected.');
delete sid_rooms[client.sessionId]; delete sid_rooms[client.id];
callback(affected_clients.array()); callback(affected_clients.array());
} }
@ -122,9 +122,9 @@ exports.room_clients_other_than_me = function(room, client) {
//gets the current room of the client (assumes one room -- will select first one if in multiple) //gets the current room of the client (assumes one room -- will select first one if in multiple)
exports.get_room = function (client) { exports.get_room = function (client) {
if (sid_rooms.hasOwnProperty(client.sessionId)) if (sid_rooms.hasOwnProperty(client.id))
{ {
var client_rooms = sid_rooms[client.sessionId].array(); var client_rooms = sid_rooms[client.id].array();
} }
if ( typeof(client_rooms) != undefined ) if ( typeof(client_rooms) != undefined )
@ -143,7 +143,7 @@ exports.add_to_room_and_announce = function (client, room, msg) {
// Broadcast new-user notification // Broadcast new-user notification
for (var i = 0; i < clients.length; i++) for (var i = 0; i < clients.length; i++)
{ {
if (clients[i].sessionId != client.sessionId) if (clients[i].id != client.id)
clients[i].send(msg); clients[i].send(msg);
} }
}); });
@ -170,7 +170,7 @@ exports.remove_from_all_rooms_and_announce = function (client, msg) {
exports.remove_from_all_rooms(client, function(clients) { exports.remove_from_all_rooms(client, function(clients) {
for (var i = 0; i < clients.length; i++) for (var i = 0; i < clients.length; i++)
{ {
if (clients[i].sessionId != client.sessionId) if (clients[i].id != client.id)
clients[i].send(msg); clients[i].send(msg);
} }
}); });
@ -199,9 +199,9 @@ exports.broadcast_room = function(room, msg) {
exports.broadcast_to_roommates = function (client, msg) { exports.broadcast_to_roommates = function (client, msg) {
var roommates = new sets.Set(); var roommates = new sets.Set();
if (sid_rooms.hasOwnProperty(client.sessionId)) if (sid_rooms.hasOwnProperty(client.id))
{ {
var client_rooms = sid_rooms[client.sessionId].array(); var client_rooms = sid_rooms[client.id].array();
for (var i = 0; i < client_rooms.length; i++) for (var i = 0; i < client_rooms.length; i++)
{ {
var room = client_rooms[i]; var room = client_rooms[i];
@ -218,12 +218,12 @@ exports.broadcast_to_roommates = function (client, msg) {
roommates.remove(client); roommates.remove(client);
roommates = roommates.array(); roommates = roommates.array();
console.log('client: ' + client.sessionId + " is broadcasting to: "); console.log('client: ' + client.id + " is broadcasting to: ");
for (var i = 0; i < roommates.length; i++) for (var i = 0; i < roommates.length; i++)
{ {
console.log(' - ' + roommates[i].sessionId); console.log(' - ' + roommates[i].id);
roommates[i].send(msg); roommates[i].send(msg);
} }
} }

28
package.json Normal file
View file

@ -0,0 +1,28 @@
{
"name": "scrumblr",
"description": "Web-based simulation of a physical agile sprint board that supports real-time collaboration.",
"version": "0.1.0",
"repository": {
"url": "http://github.com/aliasaria/scrumblr"
},
"author": "Ali Asaria",
"main": "server.js",
"directories": {
"lib": "lib/"
},
"engines": {
"node": "0.4.7"
},
"dependencies": {
"async": "0.1.x",
"connect": "1.7.x",
"redis-client": "0.3.x",
"redis": "0.6.x",
"sanitizer": "0.0.x",
"socket.io": "0.8.x",
"simplesets": "1.1.x",
"connect-redis":"1.0.x",
"express": "2.4.x",
"jade": "0.14.x"
}
}

View file

@ -1,5 +1,4 @@
var http = require('http'), var http = require('http'),
io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io')
express = require('express'), express = require('express'),
connect = require('connect'); connect = require('connect');
@ -73,10 +72,18 @@ app.listen(process.argv[2] || 8124);
var socketio_options = { var socketio_options = {
transports: ['websocket', 'flashsocket', 'htmlfile', 'jsonp-polling'] transports: ['websocket', 'flashsocket', 'htmlfile', 'jsonp-polling']
}; };
// socket.io SETUP // socket.io SETUP
var socket = io.listen(app, socketio_options); var io = require('socket.io').listen(app);
socket.on('connection', function(client){ io.configure(function () {
io.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
// , 'xhr-polling'
, 'jsonp-polling'
]);
});
io.sockets.on('connection', function (client) {
// new client is here! // new client is here!
//console.dir(client.request.headers); //console.dir(client.request.headers);
// //
@ -127,7 +134,7 @@ function scrub( text ) {
joinRoom(client, message.data, function(clients) { joinRoom(client, message.data, function(clients) {
client.send( { action: 'roomAccept', data: '' } ); client.json.send( { action: 'roomAccept', data: '' } );
}); });
@ -278,7 +285,7 @@ function scrub( text ) {
var msg = {}; var msg = {};
msg.action = 'nameChangeAnnounce'; msg.action = 'nameChangeAnnounce';
msg.data = { sid: client.sessionId, user_name: clean_message.data }; msg.data = { sid: client.id, user_name: clean_message.data };
broadcastToRoom( client, msg ); broadcastToRoom( client, msg );
break; break;
@ -333,7 +340,7 @@ function initClient ( client )
db.getAllCards( room , function (cards) { db.getAllCards( room , function (cards) {
client.send( client.json.send(
{ {
action: 'initCards', action: 'initCards',
data: cards data: cards
@ -344,7 +351,7 @@ function initClient ( client )
db.getAllColumns ( room, function (columns) { db.getAllColumns ( room, function (columns) {
client.send( client.json.send(
{ {
action: 'initColumns', action: 'initColumns',
data: columns data: columns
@ -357,7 +364,7 @@ function initClient ( client )
if (theme == null) theme = 'bigcards'; if (theme == null) theme = 'bigcards';
client.send( client.json.send(
{ {
action: 'changeTheme', action: 'changeTheme',
data: theme data: theme
@ -368,7 +375,7 @@ function initClient ( client )
db.getBoardSize( room, function(size) { db.getBoardSize( room, function(size) {
if (size != null) { if (size != null) {
client.send( client.json.send(
{ {
action: 'setBoardSize', action: 'setBoardSize',
data: size data: size
@ -383,18 +390,18 @@ function initClient ( client )
var j = 0; var j = 0;
for (i in roommates_clients) for (i in roommates_clients)
{ {
if (roommates_clients[i].sessionId != client.sessionId) if (roommates_clients[i].id != client.id)
{ {
roommates[j] = { roommates[j] = {
sid: roommates_clients[i].sessionId, sid: roommates_clients[i].id,
user_name: sids_to_user_names[roommates_clients[i].sessionId] user_name: sids_to_user_names[roommates_clients[i].id]
}; };
j++; j++;
} }
} }
console.log('initialusers: ' + roommates); console.log('initialusers: ' + roommates);
client.send( client.json.send(
{ {
action: 'initialUsers', action: 'initialUsers',
data: roommates data: roommates
@ -409,7 +416,7 @@ function joinRoom (client, room, successFunction)
{ {
var msg = {}; var msg = {};
msg.action = 'join-announce'; msg.action = 'join-announce';
msg.data = { sid: client.sessionId, user_name: client.user_name }; msg.data = { sid: client.id, user_name: client.user_name };
rooms.add_to_room_and_announce(client, room, msg); rooms.add_to_room_and_announce(client, room, msg);
successFunction(); successFunction();
@ -417,13 +424,13 @@ function joinRoom (client, room, successFunction)
function leaveRoom (client) function leaveRoom (client)
{ {
console.log (client.sessionId + ' just left'); console.log (client.id + ' just left');
var msg = {}; var msg = {};
msg.action = 'leave-announce'; msg.action = 'leave-announce';
msg.data = { sid: client.sessionId }; msg.data = { sid: client.id };
rooms.remove_from_all_rooms_and_announce(client, msg); rooms.remove_from_all_rooms_and_announce(client, msg);
delete sids_to_user_names[client.sessionId]; delete sids_to_user_names[client.id];
} }
function broadcastToRoom ( client, message ) { function broadcastToRoom ( client, message ) {
@ -457,7 +464,7 @@ function roundRand( max )
function getRoom( client , callback ) function getRoom( client , callback )
{ {
room = rooms.get_room( client ); room = rooms.get_room( client );
//console.log( 'client: ' + client.sessionId + " is in " + room); //console.log( 'client: ' + client.id + " is in " + room);
callback(room); callback(room);
} }
@ -465,7 +472,7 @@ function getRoom( client , callback )
function setUserName ( client, name ) function setUserName ( client, name )
{ {
client.user_name = name; client.user_name = name;
sids_to_user_names[client.sessionId] = name; sids_to_user_names[client.id] = name;
console.log('sids to user names: '); console.log('sids to user names: ');
console.dir(sids_to_user_names); console.dir(sids_to_user_names);
} }