如何使用connect-sqlite3与express和websockets共享会话?

时间:2018-03-09 10:01:50

标签: node.js express sqlite

我需要使用connect-sqlite3在(' connection')上的express-session(v4)GET请求和WebSockets之间共享会话。

以下没有sqlite3的基本设置(来自Azmisov在ExpressJS & Websocket & session sharing的回答)有效:

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 + ".");
    });
});

但是,当我尝试使用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 + ".");
    });
});

在sessionParser(req,{},function(){...)的wss on(' connection')中,我得到:

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.

如果我更改为:

,仍会出现此错误
sessionParser(req, {secret: 'some secret', resave: false, saveUninitialized: true}, function(){

如果我拿出" req":

,错误就会消失
sessionParser({secret: 'some secret', resave: false, saveUninitialized: true}, function(){

但是后来没有调用sessionParser函数,并且控制台中没有显示req.session.working。

会话确实会保存到sqlite3数据库。

非常感谢任何帮助,谢谢! Eric T。

1 个答案:

答案 0 :(得分:0)

我设法在websocket上使用cookie-parser和cookie修复它(' connection')。

请注意,我必须在app.use()之外声明MySessionStore,以便我可以在其上执行get()。

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;});

});