简体   繁体   中英

how to keep connect Web Socket?

we will send and receive between client and server , use web socket.

but I go to other page from web socket created page, socket connection close.

and I call form action, socket will going to reconnection.

how to keep socket connection. we need to keep socket connection, when user want.

You can't.

A websocket is always bound to the currently loaded HMTL document. When the document is closed, your websocket gets closed too. When you need a continuous communication channel with the user, you will need to do some refactoring.

Either live with it and create a new WS connection for every request. You will need some session handling then. You could do that by giving the client a randomly-generated session-ID when the WS connection is established and have the client store it in localstorage. When the server notices that a websocket connection gets closed, persist the state of the application in a way that it can be reobtained using the session-id. When the client navigates to another document, have them retrieve the session-ID from localstorage and send that session-ID via websocket so the server-sided application can assign the stored session-state to the connection.

Or design your website as a single-page application where everything happens in the same HTML document and content is switched out with AJAX calls when the user navigates (but understand the implications for search-engines and people who like to set deep-links to your content).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM