javascript - socket.io parse connect (>= 2.4.1) signed session cookie - Stack Overflow

With the latest version of connect (as of 2012-07-26), I've found the following way to get a sessi

With the latest version of connect (as of 2012-07-26), I've found the following way to get a session ID from socket.io that will work with a connect-redis store.

var express = require('express')
, routes = require('./routes')
, fs = require('fs')
, http = require('http')
, io = require('socket.io')
, redis = require('connect-redis')
, connect = require('express/node_modules/connect')
, parseSignedCookie = connect.utils.parseSignedCookie
, cookie = require('express/node_modules/cookie');

var secret = '...';
var rStore = new(require('connect-redis')(express));

//...

var server = http.createServer(app);
var sio = io.listen(server);

sio.set('authorization', function(data, accept) {
    if(data.headers.cookie) {
        data.cookie = cookie.parse(data.headers.cookie);
        data.sessionID = parseSignedCookie(data.cookie['connect.sid'], secret);
    } else {
        return accept('No cookie transmitted', false);
    }
    accept(null, true);
});

data.sessionID can then be used later such as

sio.sockets.on('connection', function(socket) {
    console.log('New socket connection with ID: ' + socket.handshake.sessionID);
    rStore.get(socket.handshake.sessionID, function(err, session) {
        //...
    });
});

Having to import so many from express (connect, a utility of connect, and the cookie module) seems like an overly roundabout way of getting the functions needed to parse connect's signed cookies. Has anyone found another way?

With the latest version of connect (as of 2012-07-26), I've found the following way to get a session ID from socket.io that will work with a connect-redis store.

var express = require('express')
, routes = require('./routes')
, fs = require('fs')
, http = require('http')
, io = require('socket.io')
, redis = require('connect-redis')
, connect = require('express/node_modules/connect')
, parseSignedCookie = connect.utils.parseSignedCookie
, cookie = require('express/node_modules/cookie');

var secret = '...';
var rStore = new(require('connect-redis')(express));

//...

var server = http.createServer(app);
var sio = io.listen(server);

sio.set('authorization', function(data, accept) {
    if(data.headers.cookie) {
        data.cookie = cookie.parse(data.headers.cookie);
        data.sessionID = parseSignedCookie(data.cookie['connect.sid'], secret);
    } else {
        return accept('No cookie transmitted', false);
    }
    accept(null, true);
});

data.sessionID can then be used later such as

sio.sockets.on('connection', function(socket) {
    console.log('New socket connection with ID: ' + socket.handshake.sessionID);
    rStore.get(socket.handshake.sessionID, function(err, session) {
        //...
    });
});

Having to import so many from express (connect, a utility of connect, and the cookie module) seems like an overly roundabout way of getting the functions needed to parse connect's signed cookies. Has anyone found another way?

Share Improve this question edited May 25, 2023 at 15:49 aynber 23k9 gold badges54 silver badges68 bronze badges asked Jul 27, 2012 at 3:19 Robert Winslow DalpeRobert Winslow Dalpe 1211 silver badge4 bronze badges 1
  • 2 I wish I could upvote this 100+ times. Your solution above saved me lots of time, thank you for posting it! I'm sorry I can't suggest another way. – LDK Commented Aug 10, 2012 at 0:40
Add a ment  | 

1 Answer 1

Reset to default 6

I was running into the same and just wrote a tiny module to abstract it. Here's how its usage looks like. It was written and tested using express 3 so should work fine with connect 2.4.x. Please let me know otherwise.

var SessionSockets = require('session.socket.io')
  , sessionSockets = new SessionSockets(io, sessionStore, cookieParser);

sessionSockets.on('connection', function (err, socket, session) {
  //your regular socket.io code goes here
});

For more details on how it works see https://github./wcamarao/session.socket.io

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744297544a4567335.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信