* A store for persisting sessions inbetween connection / disconnection. * Automatically creates session IDs on inserted objects.
(namespace, db, sockets, options)
| 23 | */ |
| 24 | |
| 25 | function SessionStore(namespace, db, sockets, options) { |
| 26 | var self = this; |
| 27 | |
| 28 | // unique id for this store in order to identify in a cluster |
| 29 | this.id = this.createUniqueIdentifier(); |
| 30 | |
| 31 | this.sockets = sockets; |
| 32 | this.options = options || {}; |
| 33 | // sessions inactive for longer than this will be cleaned up: |
| 34 | this.options.maxAge = this.options.maxAge || 30 * 24 * 60 * 60 * 1000; |
| 35 | |
| 36 | if (this.options.pubClient && this.options.subClient) { |
| 37 | debug('using pub/sub mode'); |
| 38 | this.pubClient = this.options.pubClient; |
| 39 | this.subClient = this.options.subClient; |
| 40 | } |
| 41 | |
| 42 | // socket queue |
| 43 | var socketQueue = this.socketQueue = new EventEmitter() |
| 44 | , socketIndex = this.socketIndex = {}; |
| 45 | |
| 46 | if (sockets) { |
| 47 | if (this.subClient) { |
| 48 | // subscribe to messages regarding sessions joining/leaving rooms |
| 49 | // need to resync |
| 50 | this.subClient.subscribe('dpd#session#refreshrooms'); |
| 51 | this.subClient.subscribe('dpd#session#remove'); |
| 52 | this.subClient.on('message', function(channel, message) { |
| 53 | var data; |
| 54 | switch (channel) { |
| 55 | case 'dpd#session#refreshrooms': // another node changed rooms for a session |
| 56 | data = JSON.parse(message); |
| 57 | if (data.id !== self.id && data.sid && socketIndex[data.sid]) { |
| 58 | // if we know about this session, refresh the rooms |
| 59 | self.refreshSessionRooms(data.sid); |
| 60 | } |
| 61 | |
| 62 | break; |
| 63 | case 'dpd#session#remove': // another node removed a session |
| 64 | data = JSON.parse(message); |
| 65 | if (data.id !== self.id && data.sid && sessionIndex[data.sid]) { |
| 66 | // if we know about this session, remove it from memory |
| 67 | sessionIndex[data.sid]._leaveAllRooms(); |
| 68 | self.removeSessionFromMemory(data.sid); |
| 69 | } |
| 70 | |
| 71 | break; |
| 72 | } |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | sockets.on('connection', function(client) { |
| 77 | // NOTE: do not use set here ever, the `Cookies` api is meant to get a req, res |
| 78 | // but we are just using it for a cookie parser |
| 79 | var cookies = new Cookies(client.handshake) |
| 80 | , sid = cookies.get('sid'); |
| 81 | |
| 82 | var getSession = function(sid, fn) { |
nothing calls this directly
no test coverage detected