I’m using a expressjs web app with socket.io. They share the session (express-session)
I have an issue where the code for a GET request executes before the socket can connect
This has led to errors, from variables that are set in the socket.on("connection");
router.get("/poker/:roomID", (req, res) => {
if (req.session.ingame){
req.session.redirectNote = "You cannot access multiple games at once"
res.redirect("/home");
}else{
if (!req.session.AccountInfo.LoggedIn){
req.session.redirectNote = "You cannot access Poker whilst not logged in"
res.redirect(`/account/login?redirect=/games/poker/${req.params.roomID}`);
}else{
poker_rooms.checkroomID(req, res, req.params.roomID); //this executes, and raises an error of undefined type
}
}
});
PokerIO.on("connection", (poker_socket) => {
const session = socketstore.anysocketsetup(poker_socket, socketstore.pokersockets, 1);
//this needs to run to remove the error
console.log(session.socketids)
poker_socket.on("disconnecting", ()=> {
socketstore.anysocketdisconnect(session, poker_socket, socketstore.pokersockets, 1);
poker_rooms.pokerDisconnect(session,
poker_socket.handshake.headers.referer.split(poker_socket.handshake.headers.host)[1]);
});
});
Any Ideas?
I had a similar problem with a race condition between a socket disconnect (which would happen after) a new GET request (such as on a refresh)
This was fixed by
window.addEventListener("beforeunload", ()=>{
socket.disconnect(true);
});