How can I await the poker_socket connection after the express-response. I can only find the socket after it has been set (it is accessable from req.session.reload(()=>{req.session.socketids.poker_socket})
)
Express response to the GET request:
res.send(`<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
let poker_socket;
socket.on("nextsetup", (callback)=>{
poker_socket = io("/poker");
callback();
});
window.addEventListener("beforeunload", ()=>{
poker_socket.disconnect(true)
});
</script>`);
Poker socket behavior:
PokerIO.on("connection", async (poker_socket) => {
await sockets.anysocketconnection(poker_socket, 0);
});
In sockets.js:
async function anysocketconnection(socket, type){
return new Promise((resolve, reject) => {
socket.request.session.reload(()=>{
try{
socket.request.session.socketids[types[type]] = socket.id;
//sets the socket.id in the session.socketids.poker_socket
sockets[type].set(socket.id, socket); //sets the socketid to a poker_socket map
socket.request.session.save();
}
catch{
console.warn("Already open whilst on running");
socket.emit("refresh");
}
resolve();
});
});
}
I tried using a setInterval function to check whether the req.session.reload(()=>{req.session.socketids.poker_socket})
) is not null
. Is there a better way to achieve this?