如何與 Socket.IO 1.x 和 Express 4.x 共享會話? - How to share sessions with Socket.IO 1.x and Express 4.x?

問題:

How can I share a session with Socket.io 1.0 and Express 4.x?如何與 Socket.io 1.0 和 Express 4.x 共享會話? I use a Redis Store, but I believe it should not matter.我使用 Redis 商店,但我認爲這無關緊要。 I know I have to use a middleware to look at cookies and fetch session, but don't know how.我知道我必須使用中間件來查看 cookie 和獲取會話,但不知道如何。 I searched but could not find any working我搜索但找不到任何工作

    var RedisStore = connectRedis(expressSession);
    var session = expressSession({
        store: new RedisStore({
            client: redisClient
        }),
        secret: mysecret,
        saveUninitialized: true,
        resave: true
    });
    app.use(session);

    io.use(function(socket, next) {
        var handshake = socket.handshake;
        if (handshake.headers.cookie) {
            var str = handshake.headers.cookie;
            next();
        } else {
            next(new Error('Missing Cookies'));
        }
    });

解決方案:

參考: https://stackoom.com/en/question/1j8Dc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章