forked from wcamarao/session.socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.socket.io.js
39 lines (34 loc) · 1.33 KB
/
session.socket.io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module.exports = function (io, sessionStore, cookieParser, key) {
key = key || 'connect.sid';
function enhance_event(listener, event, callback) {
listener.on(event, function (socket) {
cookieParser(socket.handshake, {}, function (parseErr) {
sessionStore.load(findCookie(socket.handshake), function (storeErr, session) {
var err = resolve(parseErr, storeErr, session);
callback(err, socket, session);
});
});
});
}
this.on = function (event, callback) {
enhance_event(io.sockets);
};
this.of = function(namespace){
var channel = io.of(namespace);
var wrapper = Object.create(channel);
wrapper.on = function(event, callback){
enhance_event(channel, event, callback);
};
return wrapper;
};
function findCookie(handshake) {
return (handshake.secureCookies && handshake.secureCookies[key])
|| (handshake.signedCookies && handshake.signedCookies[key])
|| (handshake.cookies && handshake.cookies[key]);
}
function resolve(parseErr, storeErr, session) {
if (parseErr) return parseErr;
if (!storeErr && !session) return { error:'could not look up session by key: ' + key };
return storeErr;
}
};