Javascript在WebSocket实例化中未捕获错误 [英] Javascript doesn't catch error in WebSocket instantiation

查看:219
本文介绍了Javascript在WebSocket实例化中未捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的套接字当前抛出net :: ERR_CONNECTION_REFUSED,因为服务器未运行,我现在希望服务器这样做.

My socket currently throws net::ERR_CONNECTION_REFUSED because the server isn't running, which I want it to do at the moment.

问题是以下代码无法捕获该错误.在控制台中,我在第2行看到了一个异常(net :: ERR_CONNECTION_REFUSED),我相信这不会发生,因为它在try语句中.

The problem is that the following piece of code doesn't catch the error. In the console I see an exception on line 2 (with net::ERR_CONNECTION_REFUSED) which I believe shouldn't happen as it's within a try statement.

1  try {
2    ws = new WebSocket('ws://'+ host + ':' + port + '/');
3  }
4  catch (err) {
5    console.log('This never prints');
6  }
7  ws.onerror = function (error) {
8    console.log(error);
9  };

所以我的问题是为什么它没有被抓住?

So my question is why is it not being caught?

我最终想要的是将错误消息显示在其他位置,但我无法捕获它,并且第8行打印了一个未提及net :: ERR_CONNECTION_REFUSED的事件"对象,所以我不确定如何以获得错误消息.

What I ultimately want is the error message to be displayed elsewhere, but I can't catch it, and line 8 prints an "event" object which doesn't mention net::ERR_CONNECTION_REFUSED, so I'm not sure how to get the error message out.

推荐答案

WebSocket 的连接时错误导致已调度事件,而不是抛出值.这是因为 throw 操作必须是同步的.为了将所有连接时错误作为抛出的错误处理, WebSocket 构造函数将需要完全暂停所有脚本执行和UI交互,直到完成整个WebSocket握手为止.而是,连接过程异步运行,从而在WebSocket连接在后台初始化时允许浏览器线程继续工作.由于该连接具有异步特性,因此 WebSocket 必须通过 error 事件报告错误,因为同步的 new WebSocket 操作已在结束时完成.异步连接任务遇到错误.

The WebSocket's connection-time error causes a dispatched event, not a thrown value. This is because throw operations must be synchronous. In order to handle all connection-time errors as thrown errors, the WebSocket constructor would need to completely suspend all script execution and UI interaction until the entire WebSocket handshake had completed. Instead, the connection process runs asynchronously, thereby allowing the browser thread to continue working while the WebSocket connection initializes in the background. Because of the connection's asynchronous nature, the WebSocket must report errors via error events, since the synchronous new WebSocket operation has already finished by the time the asynchronous connection task encounters an error.

您看到的 ERR_CONNECTION_REFUSED 消息纯粹是为了开发人员的利益;脚本无法以任何方式访问它.它在JavaScript环境中没有任何表示形式.只是红色的消息出现在您的控制台中,用于通知正在查看浏览器的人有关错误.

The ERR_CONNECTION_REFUSED message you see is purely for the benefit of developers; it is not accessible to the script in any way. It does not have any representation within the JavaScript environment. It's just a red-colored message that appears in your console to inform you, the human looking at the browser, about an error.

error 处理程序事件是响应失败的正确位置,但是由于设计原因,缺少脚本可读的连接时错误信息.从 WebSocket API的WHATWG规范:

The error handler event is the correct place to respond to failure, but the lack of script-readable connection-time error information is by design. From the WHATWG spec for the WebSocket API:

用户代理不得以允许脚本区分以下情况的方式向脚本传达任何故障信息:

User agents must not convey any failure information to scripts in a way that would allow a script to distinguish the following situations:

  • 无法解析其主机名的服务器.
  • 无法将数据包成功路由到的服务器.
  • 拒绝指定端口上的连接的服务器.
  • 无法正确执行TLS握手的服务器(例如,无法验证服务器证书).
  • 未完成打开握手的服务器(例如,因为它不是WebSocket服务器).
  • WebSocket服务器发送了正确的打开握手信号,但是指定了导致客户端断开连接的选项(例如,服务器指定了客户端未提供的子协议).
  • 成功完成打开握手后突然关闭连接的WebSocket服务器.

[...]允许脚本区分这些情况,将允许脚本探测用户的本地网络以为攻击做准备.

[...] Allowing a script to distinguish these cases would allow a script to probe the user's local network in preparation for an attack.

浏览器故意忽略了规范要求的任何有用信息.规范作者担心,访问此信息可能会使恶意网页获得有关您的网络的信息,因此他们要求浏览器以无法区分的方式报告所有连接时错误.

The browser is deliberately omitting any useful information as required by the spec. The spec authors are concerned that access to this information could allow a malicious Web page to gain information about your network, so they require browsers report all connection-time errors in an indistinguishable way.

这篇关于Javascript在WebSocket实例化中未捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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