保存在socket.io中的明确课程

发布于 2025-01-18 08:41:12 字数 2318 浏览 4 评论 0原文

我不明白为什么我的会议不想被保存。

每次连接时,我都会获得一个新的会话ID,但是如果我(通过浏览器)转到tessocket.local。局限性,将会保存会话,并且在通过套接字调用时,则通常会确定它。这实际上是一个问题 - 如何打败它?

强迫会话在io.on之后保存(“连接”

济 会话根本不是通过套接字定义

const socketOptions =  {

    "force new connection" : true,
    "reconnectionAttempts": "Infinity", 
    "timeout" : 10000,
    "transports" : ["websocket"]
};
// @ts-ignore
socket = io('https://tessocket.local',socketOptions)
 // @ts-ignore
socket.onAny((event, ...args) => {
    console.log(event, args);
  });
return socket

 const app = require('express')(),
  http = require('http'),
  https = require('https'),
  // http  = require("http"),
  fs = require( "fs" ),
  path = require("path"),
  eSession = require("express-session"),
  MongoDBStore = require('express-mongodb-session')(eSession),
  store = new MongoDBStore({
    uri: 'mongodb://admin:admin@localhost:27017/tmp?authSource=admin',
    collection: 'sessions'
  });
 
 
    options = {
    key: fs.readFileSync(path.resolve(__dirname, "../minica/test.local/key.pem")),
    cert: fs.readFileSync(path.resolve(__dirname, "../minica/test.local/cert.pem")),
    },
    server =  https.createServer(options, app)

  const io = require("socket.io")(server, {
    cors: "http://test.local:3000",
    transports: ["websocket"]
  }),
  session = eSession({
    secret: "my-secret",
    resave: true,
    saveUninitialized: true,
    store,
    cookie: {
      httpOnly: false, // key
      maxAge: null,
      path: "/",
      secure: true,
      sameSite: 'none'
    },
  }),
  sharedsession = require("express-socket.io-session");


// Attach session
app.use(session);

// Share session with io sockets

io.use(sharedsession(session));

io.on("connection", function(socket) {
    // Accept a login event with user's data
    console.log(socket.handshake.session.id) 
    socket.on("login", function(userdata) {
        socket.handshake.session.userdata = userdata;
        socket.handshake.session.save();
    });
    socket.on("logout", function(userdata) {
        if (socket.handshake.session.userdata) {
            delete socket.handshake.session.userdata;
            socket.handshake.session.save();
        }
    });        
});

server.listen(443);

的 ]

express@4

I can not understand why my sessions do not want to be saved.

Each time I connect, I get a new session id, but if I just go (through the browser) to tessocket.local, the session is saved and already when called through the socket, it is normally determined. That's actually the question - how to defeat it?

Forcing the session to be saved after io.on("connection" doesn't help.

If you remove transports: ["websocket"] - polling starts working, but then the session is not defined at all through the socket.

client.js

const socketOptions =  {

    "force new connection" : true,
    "reconnectionAttempts": "Infinity", 
    "timeout" : 10000,
    "transports" : ["websocket"]
};
// @ts-ignore
socket = io('https://tessocket.local',socketOptions)
 // @ts-ignore
socket.onAny((event, ...args) => {
    console.log(event, args);
  });
return socket

server.js

 const app = require('express')(),
  http = require('http'),
  https = require('https'),
  // http  = require("http"),
  fs = require( "fs" ),
  path = require("path"),
  eSession = require("express-session"),
  MongoDBStore = require('express-mongodb-session')(eSession),
  store = new MongoDBStore({
    uri: 'mongodb://admin:admin@localhost:27017/tmp?authSource=admin',
    collection: 'sessions'
  });
 
 
    options = {
    key: fs.readFileSync(path.resolve(__dirname, "../minica/test.local/key.pem")),
    cert: fs.readFileSync(path.resolve(__dirname, "../minica/test.local/cert.pem")),
    },
    server =  https.createServer(options, app)

  const io = require("socket.io")(server, {
    cors: "http://test.local:3000",
    transports: ["websocket"]
  }),
  session = eSession({
    secret: "my-secret",
    resave: true,
    saveUninitialized: true,
    store,
    cookie: {
      httpOnly: false, // key
      maxAge: null,
      path: "/",
      secure: true,
      sameSite: 'none'
    },
  }),
  sharedsession = require("express-socket.io-session");


// Attach session
app.use(session);

// Share session with io sockets

io.use(sharedsession(session));

io.on("connection", function(socket) {
    // Accept a login event with user's data
    console.log(socket.handshake.session.id) 
    socket.on("login", function(userdata) {
        socket.handshake.session.userdata = userdata;
        socket.handshake.session.save();
    });
    socket.on("logout", function(userdata) {
        if (socket.handshake.session.userdata) {
            delete socket.handshake.session.userdata;
            socket.handshake.session.save();
        }
    });        
});

server.listen(443);

[email protected]

express@4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文