Problem with front-end websocket shutting down ws.close ()

I haven"t used websocket much before. This time the supplier decided to use this. Here is the client. I checked some information. It seems that there are two ways for websocket to disconnect. One is that the client sends a request to the server, and the server responds to shut down.

var ws=new WebSocket("ws://127.0.0.1:8000");
ws.onopen=function(){
  ws.close(); 
};
ws.onclose=function(e){
  console.log(e);
};

or the server follows certain logic, such as sending close, client when data is returned, the client closes in onclose,

var ws=new WebSocket("ws://127.0.0.1:8000");
ws.onclose=function(e){
  console.log(e);
  ws.close(); //TCP
};

does not take the initiative to close at first, that is, it does not use ws.close () to close it, although it seems to close automatically, but it seems that one is not disconnected, and then sending the second will be a problem. If I don"t know whether the server will shut down actively, will there be a problem if I write ws.close () in both onopen and onclose?

Oct.16,2021

also note that you need to regularly send heartbeat packets to the server to prove that the client is alive


this is related to the websocket code in the background

normally: websocket will be disconnected automatically when you leave the page

vue can also manually close

by calling websock.close () during the destroy life cycle.

I want to turn it off, stop it from continuing, and then enable it when I want to use it. Because my example is that the current connection request mq is a topic, and then after switching to another topic, it should be turned off directly to prevent it from consuming resources in the background. How do I use this? Can I just close it? Will it connect automatically?

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35d3a-4ec2c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35d3a-4ec2c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?