Skip to content

Commit

Permalink
experimental connect/express session support
Browse files Browse the repository at this point in the history
  • Loading branch information
ericz committed Jan 15, 2012
1 parent db4cfb0 commit 501e393
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/now.js
Expand Up @@ -12,12 +12,14 @@ var Now = function () {
this.closures = {};
this.groups = {};
this.users = {};
this.sessions = {};
this.options = {
clientWrite: true,
autoHost: true,
socketio: {"log level" : 1},
client: {},
scope: 'window',
cookieKey: 'connect.sid',
closureTimeout: 30000
};
};
Expand Down Expand Up @@ -202,6 +204,14 @@ Now.prototype.initialize = function (server, options) {
everyone.emit.apply(user, ['disconnect']);
});

// Detect connect and add session middleware as necessary
// Use `in` so we look up the prototype chain
if('use' in server && 'stack' in server && 'route' in server) {
server.use(function(req, res, next) {
self.sessions[req.sessionID] = req.session;
});
}

return everyone;
};

Expand Down
11 changes: 10 additions & 1 deletion lib/user.js
Expand Up @@ -49,7 +49,16 @@ exports.initialize = function (nowjs) {
* @property {String} cookie The user's cookie, as determined by
* Socket.IO.
*/
this.user = { clientId: socket.id, cookie: nowUtil.parseCookie(socket.handshake.headers.cookie) };
this.user = {
clientId: socket.id,
cookie: nowUtil.parseCookie(socket.handshake.headers.cookie)
};

// Populate session by parsing cookie
var cookie = this.user.cookie[nowjs.options['cookieKey']];
if (cookie) {
this.user.session = nowjs.sessions[unescape(cookie)];
}

// set to true upon first replaceVar and emit connect event
/**
Expand Down

0 comments on commit 501e393

Please sign in to comment.