当应用程序托管在AWS上时req.session更改时Socket.handhsake.session不会更新

时间:2019-03-01 07:23:15

标签: node.js reactjs sockets amazon-ec2 socket.io

当req.session更新时,

socket.handhsake.session不会更改/更新。 当代码在我的本地PC上的本地主机上运行时,代码运行良好,但是在AWS上托管时,建立了套接字连接,但更新req.session时,握手会话未更新。

在谷歌搜索之后我尝试了几种方法,但无济于事。

后端

  const bodyParser = require('body-parser');
  var cors = require('cors')
  const mongoose = require('mongoose');
  var sharedsession = require("express-socket.io-session");


  var session = require("express-session")({
    secret: 'messi',
    resave: true,
    saveUninitialized: true,
    cookie: {
      maxAge: 10000000000000, secure: false,
      httpOnly: false
    } 
  });


const app = express();
const port = process.env.PORT || 5000;
var server = http.createServer(app);
var io = socketIo(server);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session);
app.use(express.static(path.join(__dirname, "client/build")));
io.use(sharedsession(session, {
  autoSave: true
}));


 // Cross Site Validation
app.use(cors({
  origin: ["http://localhost:3000", "http://localhost:3001"],
  credentials: true
}));


app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});

io.on('connection', (socket) => {
  console.log('socket connected', socket.id);
  var user;
  user = socket.handshake.session.userDetails;
  console.log('socket user data', socket.handshake.session);
  handler(socket, user);
 });

前端 Res Js

const socket = openSocket('http://ec2-31-223-200-100.compute- 
  1.amazonaws.com:5000');

更新 我仍然对此感到困惑,如果有人知道解决方案,那么请务必提供帮助!

我也在AWS安全组上打开了端口号5000(服务器端口),但是它不起作用。

0 个答案:

没有答案