express.js将会话从websocket共享到http

时间:2017-12-22 11:22:54

标签: node.js express websocket express-session

我无法找到如何在.get()块中读取最近在.ws()块中分配的特定会话值的方法:

const express = require('express');
const app = express();
const ws = require('express-ws')(app);
const session = require('express-session');
app.use(session({secret: 'secret', resave: true, saveUninitialized: true}));

app.get('/', function(req, res) {

    console.log(req.session.val) //undefined (would like to have "my value")

    res.end("<script>var ws = new WebSocket(window.location.href.replace('http', 'ws')); ws.send(true);</script>");
})
.ws('/', function(ws, req) {

    //trying to assign session
    req.session.val = 'my value';
});

app.listen(80);

请帮助。

0 个答案:

没有答案