I'm developing an online real time multiplayer game for which we are using the Ratchet php websocket library for real time communication. The game spans across multiple pages of a web site. How do I keep a websocket connection open as long as the client is in session? I'm using javascript for front-end and PHP for back-end. I'd prefer a technique that doesn't involve accessing the entire game part of the website from an iframe so that keeping the websocket connection outside the iframe keeps the connection alive.
Asked
Active
Viewed 894 times
1
-
You answered your own question. You have to keep the WebSocket object outside of the scope of any particular page so it stays alive through page changes. For instance, if you are using HTML5, you might consider [using `sessionStorage` to persist objects](https://stackoverflow.com/questions/6193574/) while the browser is open. – Remy Lebeau Nov 07 '17 at 03:49
-
Also relevant: [Keep connection across pages](https://stackoverflow.com/questions/42419138/keep-connection-with-socket-io/42432257#42432257) – jfriend00 Nov 07 '17 at 07:36
-
A webSocket will not stay connected when the page it was opened from changes. The browser simply won't do that. So, you either don't change the page that the webSocket was opened from (like in a single page app) or you code your server to recognize a client that is reconnecting just because the page changed (usually using some sort of cookie as a session or connection identifier). – jfriend00 Nov 07 '17 at 07:37