如何关闭 websocket 就像 chrome 窗口正在关闭以将缓冲区刷新为 0 [英] how to close a websocket like if chrome window was closing to flush the buffer to 0

查看:122
本文介绍了如何关闭 websocket 就像 chrome 窗口正在关闭以将缓冲区刷新为 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当端口转发 websocket 通过 Internet 并仅在连接关闭时发送(在我的网络上就是这样),我首先尝试通过关闭 chrome(它有效)来关闭 websocket,所以现在我发现终止了我think 可以做和关闭浏览器一样的事情,但它只适用于有这个功能的服务器和

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

问题是close的buffer不是0而是13...

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

那么我如何使用 ws.terminate 或其他东西来执行 hard close 或其他东西以将缓冲区刷新为 0 和基本代码,例如一个下没有错误?

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://adress: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();
               
        
       
            }
}

如果您查看图片和代码,您可以看到我使用了ws.close 但它似乎没有做任何事情缓冲区不为零并且它仍然连接这是一张图片ws.close() 之后 Websocket 的状态是什么,我认为 ws.close() 等待 serverur 也关闭连接,并且当 serverur 在关闭 serverur 之前等待接收某些东西时,它会出现错误.

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.

我的 pthon serverur if never

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)

视觉解释谢谢你的帮助[![视觉][2]][2]

a visual explication Thanks for helping [![visual][2]][2]

推荐答案

Short version: 因为 WebSocket 对象没有任何 terminate 方法.

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

close 方法(您正在调用)足以关闭 websocket.您是从哪里了解到 terminate 方法的?

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

因为在您的问题中您是在谈论硬关闭"并且在您的代码中您正在执行此检查 ws.readyState == 2 我认为您正在寻找具有 ws 的东西.readyState == 3.

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.

这只是在调用 close 方法后等待一段时间的问题.

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

onclose 属性可以帮助您;尝试在脚本末尾添加以下几行

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);
}

希望这会有所帮助.

这篇关于如何关闭 websocket 就像 chrome 窗口正在关闭以将缓冲区刷新为 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆