byungkyu
byungkyu

Reputation: 639

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.

Upvotes: 0

Views: 2129

Answers (1)

Philipp
Philipp

Reputation: 69663

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).

Upvotes: 3

Related Questions