Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loss messages before setting a listener #132

Open
karikera opened this issue Mar 14, 2020 · 0 comments
Open

loss messages before setting a listener #132

karikera opened this issue Mar 14, 2020 · 0 comments

Comments

@karikera
Copy link

karikera commented Mar 14, 2020

It seems MySQL store wait for a little to get a session.
It makes loss messages

Reproduction:

  1. use a session with MySQL store,
  2. store something to the session
  3. restart the server
  4. send a message from the client immediately on it opened

Result:

  • Loss messages

source:

var express = require("express");
var expressWs = require("express-ws");
var session = require("express-session");
var ExpressMySQLSession = require("express-mysql-session");
var MySQLStore = ExpressMySQLSession(session);
var sessionStore = new MySQLStore({
    host: 'localhost',
    user: 'root',
    database: 'express_session',
    password: 'password'
});
var app = expressWs(express()).app;
app.use(session({
    secret: 'secret',
    saveUninitialized: false,
    resave: false,
    store: sessionStore
}));
app.get('/', function (req, res) {
    // store something to the session
    console.log(req.session.something);
    req.session.something = 'something';
    // this client will reconnect automatically
    res.send("\n<script>\nfunction connect()\n{\n    const ws = new WebSocket('ws://localhost:8000/api/ws');\n    ws.onclose = ev=>{\n        setTimeout(connect, 1000);\n    };\n    ws.onopen = ()=>{\n        ws.send('data');\n    };\n    ws.onmessage = ev=>{\n        console.log(ev.data);\n    };\n}\nconnect();\n</script>\n");
});
app.ws('/api/ws', function (ws, req) {
    console.log('data will received');
    ws.on('message', function (data) {
        console.log('data receivd:' + data);
    });
});
app.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant