Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

experimental connect/express session support

  • Loading branch information...
commit 501e39371fef0c6df426003614e9480b058c3af3 1 parent db4cfb0
@ericz ericz authored
Showing with 20 additions and 1 deletion.
  1. +10 −0 lib/now.js
  2. +10 −1 lib/user.js
View
10 lib/now.js
@@ -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
};
};
@@ -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;
};
View
11 lib/user.js
@@ -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
/**
Please sign in to comment.
Something went wrong with that request. Please try again.