问题
Okay so here goes. I am trying to to use express-session
backed by connect-redis
together with websockets/ws
. The vast majority of example code I have seen usually includes the use of Express to serve the client a websocket script of some type and then configuring the server app to use the express-session
middleware to work with session data...with websockets further down the process chain.
My client side app on the other hand immediately initiates an Opening Handshake/Upgrade request, which bypasses Express and heads straight into websockets/ws
. For this reason I am not able to work with session data through the commonly used app.use(session(...)
middleware setup.
The below code uses a setup that allows me to call express-session
inside websocket/ws
and to put the connection request through express-session
so I can get at the session data. This works as is shown by the output also below. What does not work however is backing express-session
by connect-redis
inside this setup. To check I call a redis client directly to query my redis box after express-session
has run, no keys are returned.
I have three questions:
1. Is what I am doing below the 'correct' way of integrating express-session
with websockets/ws
for the client setup described?
2. If not what should I be doing?
3. If this is a good way, is it possible to get connect-redis
working as session store?
Your insights are very welcome.
Server-side code:
const express = require('express');
const Session = require('express-session');
const http = require('http');
const ws = require('ws');
const util = require('util');
const redis = require('redis');
const client = redis.createClient(6379, '10.0.130.10', {no_ready_check: true});
const RedisStore = require('connect-redis')(Session);
const SessionStore = new RedisStore({host: '10.0.130.10', port: '6379', ttl: 60, logErrors: true});
//const SessionStore = new Session.MemoryStore();
var session = Session({
store: SessionStore,
cookie: {secure: true, maxAge: 3600, httpOnly: true},
resave: false,
saveUninitialized: true,
secret: '12345'
});
// Define Express and WS servers
const app = express();
const server = http.createServer(app);
const wss = new ws.Server({ server });
// WS Websocket
wss.on('connection', function connection(ws, req) {
session(req, {}, function(){
let sessionId = req.sessionID;
console.log('SessionID: ' + sessionId);
let sessionCookie = req.session.cookie;
console.log('SessionCookie: ' + JSON.stringify(sessionCookie));
});
client.keys('*', function(err, reply) {
// reply is null when the key is missing
console.log('Redis reponse: ' + reply);
});
});
server.listen(10031, function listening() {
console.log('Listening on: ' + server.address().port);
});
Server-side console.log output (note the Redis response):
Listening on: 10031
SessionID: Oi8AdsoZTAm3hRLmxPfGo43Kmmj_Yd6F
SessionCookie: {"originalMaxAge":3599,"expires":"2017-05-29T17:45:54.467Z","secure":true,"httpOnly":true,"path":"/"}
Redis reponse:
Reference: express-session, connect-redis and einaros/ws Reference: ExpressJS & Websocket & session sharing
来源:https://stackoverflow.com/questions/44248437/calling-express-session-backed-by-connect-redis-inside-websockets-ws