简体   繁体   English

如何在客户端浏览器中中止“新 WebSocket”

[英]How to abort a “new WebSocket” in a client browser

Javascript new Websocket(socketUrl) runs on potentially forever if the server refuses to open the websocket, and by design, the surrounding script gets no information on why the server refuses to open it.如果服务器拒绝打开 websocket,Javascript new Websocket(socketUrl)可能会永远运行,并且根据设计,周围的脚本不会获得有关服务器拒绝打开它的原因的信息。 I have seen client browsers sending websocket requests every few seconds for more than 10 weeks (I suppose a tab has been left open and unused for this whole time).我已经看到客户端浏览器每隔几秒钟发送 websocket 请求超过 10 周(我想一个标签一直处于打开状态并且一直未使用)。

I have implemented a change in the surrounding client script that detects (via a normal http request) when the websocket requests have become useless, and then closes the socket.我已经对周围的客户端脚本进行了更改,当 websocket 请求变得无用时,它检测(通过正常的 http 请求),然后关闭套接字。 From this point onwards, clients who reload their javascript files won't cause problems anymore.从此时起,重新加载其 javascript 文件的客户端将不再引起问题。

However, I wonder if there is a way to definitely deal with those forgotten tabs in clients who don't ever reload, or if the only thing I can do is to basically treat them like malware by blocking excess requests in the load balancer (haproxy).但是,我想知道是否有一种方法可以明确地处理那些从不重新加载的客户端中被遗忘的选项卡,或者我唯一能做的就是通过阻止负载均衡器中的多余请求(haproxy )。

Lets say your websocket object is named, websocket_obj .假设您的 websocket object 被命名为websocket_obj Like:喜欢:

websocket_obj = new Websocket(url); // url You know it

Then, you can close the socket, when you get an error.然后,当您收到错误时,您可以关闭套接字。 So,所以,

websocket_obj.onerror = function(){
    Websocket.close();
}

Here is the mdn_refs .这是mdn_refs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM