快速会话,connect-redis和einaros/ws [英] express-session, connect-redis and einaros/ws

查看:108
本文介绍了快速会话,connect-redis和einaros/ws的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎很难让Express,express-session,connect-redis和websockets/ws一起很好地玩.总体而言,这很可能与我对这些模块和编码的有限理解有关.此处的大多数代码均来自存储库中的各个示例以及其他试图完成类似任务的资源.

I seem to be having some trouble getting Express, express-session, connect-redis and websockets/ws to play together nicely. This most likely has to do with my as yet limited understanding of these modules and coding in general. Most of the code here is taken from the respective examples in the repositories as well as other sources trying to accomplish something similar.

因此,这里我们有一个启用了websocket的应用程序,试图立即启动与服务器端应用程序的websocket连接,该连接什么都不做,只提供当前的websocket功能.该请求首先命中我们的NGINX API代理,该代理将请求代理到本地运行的Node应用程序实例.

So what we have here is a websocket-enabled app trying to immediately initiate a websocket connection to the server-side app, which does nothing but provide websocket functions currently. This request first hits our NGINX API proxy, which proxies the request to a locally running instance of the Node app.

应将Node应用程序配置为接受并立即将请求升级到websocket,创建唯一的会话ID和cookie,然后将其存储在我们的Redis框中. Redis中的所有此EXCEPT存储都可以正常运行.

The Node app should be configured to accept and immediately upgrade the request to websocket, create a unique session id and cookie, which should then be stored inside our Redis box. All of this EXCEPT storage in Redis functions correctly.

下面的代码在没有connect-redis的情况下有效(我直接使用node_redis客户端查询Redis框中的任何键):

The following code WITHOUT connect-redis works (I use the node_redis client directly to query the Redis box for any keys):

'use strict';

const express = require('express');
const http = require('http');
const ws = require('ws');
const redis = require('redis');
const client = redis.createClient(6379, '10.0.130.10', {no_ready_check: true});
//const RedisStore = require('connect-redis')(session);


var session = require('express-session')({
    //store: new RedisStore({host: '10.0.130.10', port: '6379', ttl: 60, logErrors: true}),
    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 });


// Client heartbeat
function heartbeat() {
    this.isAlive = true;
}


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

    // Obtain Client IP address from NGINX x-forwarded-for proxy header
    let clientIp = req.headers['x-forwarded-for'];

请求后的服务器输出:

听着:10031

会话ID:eavjOlls59jtjQd-gZ0EIhqf3_P6sYMr

Session ID: eavjOlls59jtjQd-gZ0EIhqf3_P6sYMr

会话Cookie:{"originalMaxAge":3600,"expires":"2017-05- 27T13:59:19.663Z,"安全:true," httpOnly:true,"路径:"/}

Session Cookie: {"originalMaxAge":3600,"expires":"2017-05- 27T13:59:19.663Z","secure":true,"httpOnly":true,"path":"/"}

Redis对索引键的响应:

Redis reponse for index keys:

通过取消对相关部分的注释来启用connect-redis会导致服务器启动时在服务器上出现以下错误消息:

Enabling connect-redis by uncommenting the relevant sections leads to the following error message on the server when started:

/home/ubuntu/app/njs/nl-websocket-dev/node_modules/connect-redis/lib/connect-redis.js:39 var Store = session.Store; ^

/home/ubuntu/app/njs/nl-websocket-dev/node_modules/connect-redis/lib/connect-redis.js:39 var Store = session.Store; ^

TypeError:无法读取未定义的属性"Store" 在module.exports(/home/ubuntu/app/njs/nl-websocket-dev/node_modules/connect-redis/lib/connect-redis.js:39:22) 在对象. (/home/ubuntu/app/njs/nl-websocket-dev/stackoverflow.js:8:48) 在Module._compile(module.js:409:26) 在Object.Module._extensions..js(module.js:416:10) 在Module.load(module.js:343:32) 在Function.Module._load(module.js:300:12) 在Function.Module.runMain(module.js:441:10) 在启动时(node.js:139:18) 在node.js:968:3

TypeError: Cannot read property 'Store' of undefined at module.exports (/home/ubuntu/app/njs/nl-websocket-dev/node_modules/connect-redis/lib/connect-redis.js:39:22) at Object. (/home/ubuntu/app/njs/nl-websocket-dev/stackoverflow.js:8:48) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at startup (node.js:139:18) at node.js:968:3

请注意,我以这种方式启动了快速会话,因此可以将其绑定到websockets/ws中.我已经尝试了很多配置,而这实际上是允许express-session和websockets/ws一起工作的唯一配置.我从这里的"Azmisov"获得要点(尤其是不包括connect-redis):

Note that I initiated express-session in the way I did so I could tie it into websockets/ws. I've tried quite a few configurations, and this is literally the only configuration that allowed express-session and websockets/ws to work together. I got the gist from 'Azmisov' here (which notably excludes connect-redis):

ExpressJS& Websocket&会话共享

以这种方式将express-session调用到websockets/ws中确实起作用.但是,使用此配置,问题就从此开始,将connect-redis添加到express-session上,然后在websockets/ws中调用该组合.

Calling express-session into websockets/ws in this manner does function. However, adding connect-redis onto express-session and then calling that combination inside websockets/ws is where the problems start, using this configuration.

任何关于如何使它工作的想法都受到欢迎.

Any thoughts on how to get this working are most welcome.

推荐答案

您在此处使用session:

const RedisStore = require('connect-redis')(session);

稍后在这里定义哪个:

var session = require('express-session')(...);

这是第一个问题(session未定义).

So that's the first issue (session being undefined).

另一个问题是,当您的代码试图将其的实例化版本传递给require('express-session')时,应该将connect-redis传递给它.

The other issue is that connect-redis should be passed the result of require('express-session'), while your code is trying to pass it an instantiated version of it.

两个问题都可以这样解决:

Both issues can be solved like this:

const Session    = require('express-session');
const RedisStore = require('connect-redis')(Session);

var session = Session({
  store: new RedisStore(...),
  ...
});

在发布的代码中的任何地方,我也看不到session是如何附加到app的,但是它还没有完成,所以我认为您正在调用app.use(session)的某个地方.

I also don't see how session is being attached to app anywhere in the code you post, but it's not complete so I assume that somewhere you're calling app.use(session).

这篇关于快速会话,connect-redis和einaros/ws的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆