通过密码1006获取websockets关闭的原因 [英] getting the reason why websockets closed with close code 1006

查看:478
本文介绍了通过密码1006获取websockets关闭的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道websockets关闭的原因,所以我可以向用户显示正确的消息。

I would like to get the reason websockets closed, so I can show the right message to the user.

我有

sok.onerror=function (evt) 
     {//since there is an error, sockets will close so...
       sok.onclose=function(e){
           console.log("WebSocket Error: " , e);}

代码总是1006,原因总是。但是我想区分不同的结果原因。

The code is always 1006 and the reason is always " ". But I want to tell different closing reasons apart.

例如,命令行给出错误原因:你不能删除它,因为数据库不会让你。但在Chrome的控制台上,原因仍然是。

For example the comand line gives an error reason : "you cannot delete that, because database wont let you". But on Chrome's console, the reason is still " ".

除了说明不同的结算原因之外的任何其他方式?

Any other way to tell different closing reasons apart?

推荐答案

关闭代码 1006 是一个特殊代码这意味着浏览器实现异常(本地)连接已关闭。

Close Code 1006 is a special code that means the connection was closed abnormally (locally) by the browser implementation.

如果您的浏览器客户端报告关闭代码 1006 ,那么您应该查看 websocket.onerror(evt)事件以获取详细信息。

If your browser client reports close code 1006, then you should be looking at the websocket.onerror(evt) event for details.

然而,Chrome很少会向javascript方面报告任何密切的代码1006个原因。这可能是由于WebSocket规范中的客户端安全规则,以防止滥用websocket。 (例如使用它来扫描目标服务器上的开放端口,或者为拒绝服务攻击生成大量连接)。

However, Chrome will rarely report any close code 1006 reasons to the javascript side. This is likely due to client security rules in the WebSocket spec to prevent abusing websocket. (such as using it to scan for open ports on a destination server, or for generating lots of connections for a denial-of-service attack).

请注意Chrome将会如果在HTTP升级到Websocket期间出现错误(这是websocket在技术上连接之前的步骤),通常会报告密码 1006 。由于身份验证或授权错误或协议使用不当(例如请求子协议,但服务器本身不支持相同的子协议),甚至尝试与不是websocket的服务器位置进行通信(例如尝试连接 ws://images.google.com/

Note that Chrome will often report a close code 1006 if there is an error during the HTTP Upgrade to Websocket (this is the step before a websocket is technically "connected"). For reasons such as bad authentication or authorization, or bad protocol use (such as requesting a subprotocol, but the server itself doesn't support that same subprotocol), or even an attempt at talking to a server location that isn't a websocket (such as attempting to connect to ws://images.google.com/)

从根本上说,如果你看到一个密码 1006 ,你对websocket本身有一个非常低级别的错误(类似于无法打开文件或套接字错误),对用户来说并不是真的意味着,因为它指出了代码和实现的低级问题。修复您的低级问题,然后在连接时,您可以包含更合理的错误代码。您可以在项目的范围或严重性方面完成此任务。示例:信息和警告级别是项目特定协议的一部分,不会导致连接终止。使用严重或致命的消息报告也使用项目的协议来传达尽可能多的细节,然后使用websocket关闭流程的有限能力关闭连接。

Fundamentally, if you see a close code 1006, you have a very low level error with websocket itself (similar to "Unable to Open File" or "Socket Error"), not really meant for the user, as it points to a low level issue with your code and implementation. Fix your low level issues, and then when you are connected, you can then include more reasonable error codes. You can accomplish this in terms of scope or severity in your project. Example: info and warning level are part part of your project's specific protocol, and don't cause the connection to terminate. With severe or fatal messages reporting also using your project's protocol to convey as much detail as you want, and then closing the connection using the limited abilities of the websocket close flow.

请注意,WebSocket密码严格定义,密切原因短语/消息的长度不能超过123个字符(这是故意的websocket限制)。

Be aware that WebSocket close codes are very strictly defined, and the close reason phrase/message cannot exceed 123 characters in length (this is an intentional websocket limitation).

但不是所有这些都丢失了,如果你只是出于调试的原因想要这些信息,关闭的细节及其根本原因通常会在Chrome的javascript控制台中详细报告。

But not all is lost, if you are just wanting this information for debugging reasons, the detail of the closure, and its underlying reason is often reported with a fair amount of detail in Chrome's javascript console.

这篇关于通过密码1006获取websockets关闭的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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