Web site on different JavaScript pages

I implemented the main website client in JS:

function connectToNotifServer(){

var conn = new WebSocket('ws://localhost:8080');

conn.onopen = function(e) {
    alert("Connection established!");
    conn.send(JSON.stringify({user_id: sessionStorage.getItem("user_id")}));
};

conn.onmessage = function(e) {
    alert(e.data);
};

conn.onclose = function(e) {
    alert("Connection closed!");
};
return conn;
}

But I want to use the web socket connection on the page. However, if I just call this method on loading, it bothers me that a new connection will be made every time the page reloads, is this a problem ?. In any case, to make a connection once and use it on different pages and reload the page?

thank

+3
source share
3 answers

You can do this by opening a WebSocket connection from a common web working user.

The connection to the WebSocket will remain open if at least one tab is still active for the given source (shared web worker).

, Chrome IE11 WebSocket -. Firefox . , WebSocket - ( , ).

+1

. , , .

: " , ", . .

0

, .

0

All Articles