javascript - How to maintain a WebSockets connection between pages? -


on 1 of scripts have code:

var websocket = window.websocket || window.mozwebsocket; window.ws = new websocket('ws://64.121.210.140:2585/consoleappsample', 'my-protocol'); 

which works fine. however, when user changes pages, have re-establish connection. believe causing problems in code because if client sends data server , changes pages, data may not received , race conditions occurring.

i tried put window.ws in global scope didn't seem fix problem. there way websockets connection persist between pages connection not need reestablished?

the global scope mentioned related javascript context, , context created each windows (and destroyed when document unloaded memory). therefore, effort useless: can't keep connection opened if user change page. of course can have webapp "single page" application, data loaded using xmlhttprequest / ajax / websocket. so, leaving page means leaving / shutdown application, , makes sense close socket.

another old approach put pages in frame, user navigate in frame (even if takes whole size of window). in way, can create websocket in top window, never changed (that means url showed in location bar same).

said that, agreed @dystroy: application should able handle scenario - user have network problem , lost connection moment, if doesn't leave page.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -