how to close a websocket like if chrome window was closing to flush the buffer to 0

只愿长相守 提交于 2020-06-28 05:01:23

问题


when with portfowarding the websocket pass by internet and only send when the connexion is close (on my network it's like that), I first try to close the websocket by closing chrome(it worked), so now i found terminate that i think can do the same than closing the webbrowser,but it's only for the serveur that have this function and

the problem is that the buffer with close is not 0 but 13...

so how can i do a hard close with ws.terminate or something else to flush the buffer to 0 and a basic code like the one under without having the error?

ws = new WebSocket("ws://trillion.ddns.net:5677/"),
messages = document.createElement('ul');
ws.onopen = function (event) {
            alert("connect");
            var lol = document.getElementById('lolz').value;
              alert(lol);
            ws.send(lol+" <p>\b\n\r");
            ws.close();
            console.log(ws.readyState);



            if(ws.readyState ==2){
                alert("ws.terminate");

                return ws.terminate();



            }
}

if you look the picture and the code you can see that i usews.close but it doesn't seems to do something the buffer is not to zero and it's still connected this is a pictures of what is the status of the Websocket after ws.close(), i think that ws.close() waits that the serveur also close the connection and when the serveur waits to receive something before closing the serveur, it put's an error. my pthon serveur if never

async def Time(websocket, path):


    try:
        a=await websocket.recv()
        print("final receive:"+str(a)+"/")
        a=str(a).split("<p>")
        a=a[0]
        print("/"+str(a)+"/")
    except:
        print("erreur")
    finally:
        socketout(bytes(a.encode("utf-8")))
        print("loop stop")
        loop.stop()
        print("loop ad stop")

start_server = websockets.serve(Time, "", 5678)
loop3=asyncio.get_event_loop()   

loop3.run_until_complete(start_server)

a visual explication Thanks for helping


回答1:


Short version: because WebSocket object doesn't have any terminate method.

The close method (which you are calling) is enough to close the websocket. Where did you read about terminate method?

Since in your question you are speaking about "hard close" and in you code you are doing this check ws.readyState == 2 I think you are looking for something to have ws.readyState == 3.

To have this is just matter of waiting a while after you called the close method.

The onclose property could help you; try adding following lines at the end of your script

ws.onclose = function(event) {
  console.log("Socket hard closed", ws.readyState);
}

Hope this helps.




回答2:


The terminate method you are trying to use is from a server implementation of WebSocket (not browser).

If you feel worried about the close method not actually closing the connection, you can verify it by yourself.

In your browser's console, type let socket = new WebSocket("wss://javascript.info/article/websocket/demo/hello");

Now in a Shell, type ping javascript.info which will give you the remote IPv6: 2606:4700:20::681a:c11.

Now type netstat -an -p tcpv6 which should display something like:

Finally, type socket.close(); in the browser's console and run netstat -an -p tcpv6 again, you'll notice that the previous row has disappeared.

Also, you'll notice that the row still shows in the browser's Network tab:

So just don't rely on it when making assumptions on the socket's status.



来源:https://stackoverflow.com/questions/62304332/how-to-close-a-websocket-like-if-chrome-window-was-closing-to-flush-the-buffer-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!