简体   繁体   中英

How do I share sessions with express and websockets using connect-sqlite3?

I need to share sessions between express-session (v4) GET requests and WebSockets on('connection') using connect-sqlite3.

The following basic setup without sqlite3 (from Azmisov's answer at ExpressJS & Websocket & session sharing ) works:

global.app = express();
var sessionParser = require('express-session')({
    secret:"some secret",
    resave: true,
    saveUninitialized: true
});
app.use(sessionParser);

app.get('*', function (req, res) {
    req.session.working = "Yes";
});

wss.on('connection', function connection(ws, req) {
    sessionParser(req, {}, function(){
        console.log("New websocket connection: Working = " + req.session.working + ".");
    });
});

However, when I try to implement this with connect-sqlite3:

var sessionParser = require('express-session');
var SQLiteStore = require('connect-sqlite3')(sessionParser);
app.use(sessionParser({
    store: new SQLiteStore({dir:configuration.data_dir, db:'sessions.s3db', table:'sessions'}),
    secret: 'some secret',
    resave: false,
    saveUninitialized: true,
    cookie: {
        maxAge: configuration.session_cookie_maxage_ms,
        name: 'shadowlands'
    },
}));

app.get('*', function (req, res) {
    req.session.working = "Yes";
});

wss.on('connection', function connection(ws, req) {
    sessionParser(req, {}, function(){
        console.log("New websocket connection: Working = " + req.session.working + ".");
    });
});

In wss on('connection') at sessionParser(req, {}, function(){ ...), I get:

express-session deprecated undefined resave option; provide resave option; provide resave option.
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option; provide saveUninitialized option.
express-session deprecated req.secret; provide secret option.

This error still occurs if I change to:

sessionParser(req, {secret: 'some secret', resave: false, saveUninitialized: true}, function(){

The error goes away if I take out "req":

sessionParser({secret: 'some secret', resave: false, saveUninitialized: true}, function(){

But then the sessionParser function does not get called, and req.session.working is not shown in the console.

The session does actually get saved to the sqlite3 database.

Any help much appreciated, thanks! Eric T.

I managed to fix it using cookie-parser and cookie in the websocket on('connection').

Note that I had to declare MySessionStore outside the app.use() so that I could perform a get() on it.

var sessionParser = require('express-session');
var SQLiteStore = require('connect-sqlite3')(sessionParser);
var MySessionStore = new SQLiteStore({dir:configuration.data_dir, db:'sessions.s3db', table:'sessions'});
var cookieParser = require('cookie-parser');
var cookie = require('cookie');
app.use(cookieParser())
var sessionParser = require('express-session');
app.use(sessionParser({
    resave: true,
    saveUninitialized: true,
    secret: "some secret",
    store: MySessionStore,
    cookie: { maxAge: 7 * 24 * 60 * 60 * 1000 } // 1 week
}));

// ============

app.get('*', function (req, res) {
    req.session.working = "Yes";
});

// ============

wss.on('connection', function connection(ws, req) {

    var got_cookie = false;

    var cookies = cookie.parse(req.headers.cookie);
    var sessionID = cookieParser.signedCookie(cookies['connect.sid'], "some secret");
    MySessionStore.get(sessionID, function(err, sess) {
        functions.write_log("session", "WSS: New websocket connection: " + "Working = '" + sess.working + "'.");
        got_cookie = true;
    });

    // Optional, if you need a synchronous return from the function. 
    require('deasync').loopWhile(function(){return !got_cookie;});

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM