Skip to content

Commit

Permalink
fixed bugs preventing events from being emitted to users in certain c…
Browse files Browse the repository at this point in the history
…onnection states
  • Loading branch information
ritch committed Oct 20, 2012
1 parent 56a9348 commit 1d259b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
51 changes: 26 additions & 25 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ function SessionStore(namespace, db, sockets) {
var socketQueue = this.socketQueue = new EventEmitter()
, socketIndex = this.socketIndex = {};

// TODO - sockets.on('connection', ...) - map to a session id based on socket.handshake.headers
sockets && sockets.on('connection', function (socket) {
// NOTE: do not use set here ever, the `Cookies` api is meant to get a req, res
// but we are just using it for a cookie parser
var cookies = new Cookies(socket.handshake)
, sid = cookies.get('sid');

if(sid) {
// index sockets against their session id
socketIndex[sid] = socket;
socketQueue.emit(sid, socket);
}
});
if(sockets) {
sockets.on('connection', function (socket) {
// NOTE: do not use set here ever, the `Cookies` api is meant to get a req, res
// but we are just using it for a cookie parser
var cookies = new Cookies(socket.handshake)
, sid = cookies.get('sid');

if(sid) {
// index sockets against their session id
socketIndex[sid] = socket;
socketQueue.emit(sid, socket);
}
});
}

Store.apply(this, arguments);
}
Expand Down Expand Up @@ -104,43 +105,44 @@ SessionStore.prototype.createSession = function(sid, fn) {
*/

function Session(data, store, sockets, rawSockets) {
var sid;
this.data = data;
if(data && data.id) this.sid = data.id;
if(data && data.id) this.sid = sid = data.id;
this.store = store;

// create faux socket, to queue any events until
// a real socket is available
var socketWrapper = this.socket = {
on: function () {
var s = sockets[sid];
// if we have a real socket, use it
if(this._socket) {
this._socket.apply(this._socket, arguments);
if(s) {
s.on.apply(s, arguments);
} else {
// otherwise add to bind queue
var queue = this._bindQueue = this._bindQueue || [];
queue.push(arguments);
}
},
emit: function () {
emit: function (ev) {
var s = sockets[sid];

// if we have a real socket, use it
if(this._socket) {
this._socket.emit.apply(this._socket, arguments);
if(s) {
s.emit.apply(s, arguments);
} else {
// otherwise add to bind queue
var queue = this._emitQueue = this._bindQueue || [];
queue.push(arguments);
}
},
_socket: sockets[this.sid]
}
};

this.emitToUsers = function(collection, query, event, data) {
collection.get(query, function(users) {
var userSession;
// TODO: arguments in weird order
if(users && users.id) {
userSession = userSessionIndex[err.id];
console.info(userSession);
userSession = userSessionIndex[users.id];
if(userSession && userSession.socket) {
userSession.socket.emit(event, data);
}
Expand All @@ -163,7 +165,6 @@ function Session(data, store, sockets, rawSockets) {

// resolve queue once a socket is ready
store.socketQueue.once(this.sid, function (socket) {
socketWrapper._socket = socket;
// drain bind queue
if(socketWrapper._bindQueue && socketWrapper._bindQueue.length) {
socketWrapper._bindQueue.forEach(function (args) {
Expand Down
13 changes: 0 additions & 13 deletions test/sessions.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ describe('Session', function() {
});
}


it('should make the socket available from the session', function(done) {
var sockets = new EventEmitter()
, store = new SessionStore('sessions', db.create(TEST_DB), sockets);

store.createSession(function (err, session) {
var fauxSocket = {handshake: { headers: {cookie: 'name=value; name2=value2; sid=' + session.sid} } };
sockets.emit('connection', fauxSocket);
expect(session.socket._socket).to.equal(fauxSocket);
done(err);
});
});

it('should make sockets available even before they exist', function(done) {
this.timeout(100);

Expand Down

0 comments on commit 1d259b7

Please sign in to comment.