重定向时丢失会话(node.js + socket.io) [英] Losing session when redirect (node.js + socket.io)

查看:35
本文介绍了重定向时丢失会话(node.js + socket.io)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中间件来使用socket.io并一起表达.

I have a middleware to use socket.io and express together.

io.use(function(socket, next) {
  sessionMiddleWare(socket.request, socket.request.res, next);
});

我通过socket.io发送实时通知,一切顺利.但是在使用socket.io之后,当我重定向到另一个页面时,我的会话丢失了,特别是所有客户端的会话都相同.在我不使用socket.io之前,一切都很好.即使当我重定向到另一个页面时,我也不会丢失会话.为什么我刚在使用socket.io后丢失了会话?请帮帮我.

I send live notification via socket.io, and it's going well. But after using socket.io, and when I redirect to another page i lose my session, in particular session being same for all clients. Before i don't use socket.io all is going good. Even when i redirect to another page i don't lose session. Why i lose session exactly after using socket.io? Help me please.

推荐答案

const app = require('express')();
const sharedsession = require('express-socket.io-session');
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const session = require('express-session')({
  secret: 'my-secret',
  resave: true,
  saveUninitialized: true
});
// Share session with io sockets
io.use(sharedsession(session));

这篇关于重定向时丢失会话(node.js + socket.io)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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