Created
March 1, 2012 22:35
-
-
Save matsumotius/1953715 to your computer and use it in GitHub Desktop.
get session from socket.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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