我正在整合redis,@www.example.com/redis-adapter,socket.io和express-session,代码如下:socket.io/redis-adapter, socket.io and express-session. The following is the code :
// Promisify the methods so we can use async / await with redis
bluebird.promisifyAll(redis);
const redisClient = redis.createClient({ legacyMode: true });
const subClient = redisClient.duplicate({ legacyMode: true });
bluebird.promisifyAll(redisClient);
bluebird.promisifyAll(subClient);
const redisStore = new RedisStore({ client: redisClient });
// Applying Redis middleware
app.use(
session({
secret: process.env.SESSION_SECRET,
// create new redis store.
store: redisStore,
cookie: {
secure: !isDevEnv, // if true: only transmit cookie over https
httpOnly: true, // if true: prevents client side JS from reading the cookie
maxAge: 30 * 24 * 60 * 60 * 1000, // session max age in milliseconds
},
resave: false,
saveUninitialized: true,
rolling: true,
}),
);
在下面的代码中,redisClient
和subClient
用于传递到createAdapter函数。我在另一个名为IdentityCheck
的函数中使用redisClient
,其中我们使用该函数为用户维护单个会话,并使用函数IdentityCheck
中的redis set和get函数设置visitorId(用户ID
// Start the server
const server = app.listen(PORT, console.log(`App listening on port: ${PORT}`));
server.setTimeout(6 * 60 * 1000);
// Start socket io with cors enabled
const io = new Server();
io.adapter(adapter.createAdapter(redisClient, subClient));
(async () => {
Promise.allSettled([redisClient.connect(), subClient.connect()]).then(() => {
io.listen(server, {
cors: {
origin: [
'http://localhost:3000'
],
},
transports: ['websocket'],
});
identityCheck.init(io, redisClient); // using redisClient in other file where we use redis get, set methods
redisClient.on('error', (error) => console.error('error from redisClient', error));
subClient.on('error', (error) => console.error('error from subClient', error));
});
})();
即使我将错误处理程序设置为:
redisClient.on('error', (error) => console.error('error from redisClient', error));
subClient.on('error', (error) => console.error('error from subClient', error));
我仍然得到missing 'error' handler on this Redis client
。我在这里做错了什么吗?我正在使用以下版本:
{
"express": "^4.17.1",
"express-session": "^1.17.1",
"redis": "4.5.1",
"socket.io": "4.5.4",
"socket.io-client": "4.5.4",
"@socket.io/redis-adapter": "8.0.1",
}
Redis版本4.5.1,www.example.com,套接字. io-client-4.5.4和@www.example.com ":" 8.0.1 ", socket.io , socket.io-client-4.5.4 and @ socket.io/redis-adapter ": "8.0.1",
1条答案
按热度按时间oaxa6hgo1#
我把redis版本降级到了3.1.2,错误已经消失了!我认为这和redis版本4有关。如果有人有正确的理由,请随时回答。谢谢