Skip to content

Instantly share code, notes, and snippets.

@matsumotius
Created March 1, 2012 22:35
Show Gist options
  • Save matsumotius/1953715 to your computer and use it in GitHub Desktop.
Save matsumotius/1953715 to your computer and use it in GitHub Desktop.
get session from socket.io
var express = require('express');
var parse_cookie = require('connect').utils.parseCookie;
var MemoryStore = express.session.MemoryStore;
var session_store = new MemoryStore();
var app = module.exports = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.session({
store : session_store,
secret : 'secret-key',
key : 'express.sid'
}));
});
var io = require('socket.io').listen();
io.configure(function(){
io.set('log level', 1);
io.set('authorization', function(data, accept){
if(data.headers.cookie){
data.cookie = parse_cookie(data.headers.cookie);
data.session_id = data.cookie['express.sid'];
session_store.get(data.session_id, function(error, session){
data.session = error ? null : session;
accept(null, true);
});
} else {
data.session = null;
accept(null, true)
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment