【问题标题】:Websocket onerror - how to read error description?Websocket onerror - 如何阅读错误描述?
【发布时间】:2013-09-19 04:24:20
【问题描述】:

我一直在开发基于浏览器的多人游戏一段时间,并且一直在测试各种环境(客户办公室、公共 wifi 等)中的不同端口可访问性。一切都很顺利,除了一件事:我不知道如何阅读错误号。或收到 onerror 事件时的描述。

客户端 websocket 是用 javascript 完成的。

例如:

// Init of websocket
websocket = new WebSocket(wsUri);
websocket.onerror = OnSocketError;
...etc...

// Handler for onerror:
function OnSocketError(ev)
{
    output("Socket error: " + ev.data);
}

'output' 只是一些写入 div 的实用函数。

我得到的是 ev.data 的“未定义”。总是。我一直在谷歌搜索,但似乎没有关于此事件有哪些参数以及如何正确阅读它的规范。

感谢任何帮助!

【问题讨论】:

标签: javascript html websocket


【解决方案1】:

除了 nmaier 的回答,正如他所说的 you'll always receive code 1006。但是,如果您在理论上以某种方式收到其他代码,这里是显示结果的代码(通过RFC6455)。

在实践中你几乎永远不会得到这些代码,所以这些代码几乎毫无意义

var websocket;
if ("WebSocket" in window)
{
    websocket = new WebSocket("ws://yourDomainNameHere.org/");
    
    websocket.onopen = function (event) {
        $("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "The connection was opened");
    };
    websocket.onclose = function (event) {
        var reason;
        alert(event.code);
        // See https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1
        if (event.code == 1000)
            reason = "Normal closure, meaning that the purpose for which the connection was established has been fulfilled.";
        else if(event.code == 1001)
            reason = "An endpoint is \"going away\", such as a server going down or a browser having navigated away from a page.";
        else if(event.code == 1002)
            reason = "An endpoint is terminating the connection due to a protocol error";
        else if(event.code == 1003)
            reason = "An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).";
        else if(event.code == 1004)
            reason = "Reserved. The specific meaning might be defined in the future.";
        else if(event.code == 1005)
            reason = "No status code was actually present.";
        else if(event.code == 1006)
           reason = "The connection was closed abnormally, e.g., without sending or receiving a Close control frame";
        else if(event.code == 1007)
            reason = "An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [https://www.rfc-editor.org/rfc/rfc3629] data within a text message).";
        else if(event.code == 1008)
            reason = "An endpoint is terminating the connection because it has received a message that \"violates its policy\". This reason is given either if there is no other sutible reason, or if there is a need to hide specific details about the policy.";
        else if(event.code == 1009)
           reason = "An endpoint is terminating the connection because it has received a message that is too big for it to process.";
        else if(event.code == 1010) // Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.
            reason = "An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. <br /> Specifically, the extensions that are needed are: " + event.reason;
        else if(event.code == 1011)
            reason = "A server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.";
        else if(event.code == 1015)
            reason = "The connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified).";
        else
            reason = "Unknown reason";
        
        $("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "The connection was closed for reason: " + reason);
    };
    websocket.onmessage = function (event) {
        $("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "New message arrived: " + event.data);
    };
    websocket.onerror = function (event) {
        $("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "There was an error with your websocket.");
    };
}
else
{
    alert("Websocket is not supported by your browser");
    return;
}

websocket.send("Yo wazzup");

websocket.close();

http://jsfiddle.net/gr0bhrqr/

【讨论】:

  • 那些事件码不行,一般只能收到1006码。
  • 是的,我的回答真的是假设性的,除非他们处于可以接收更多代码的奇怪情况,我不确定为什么它有这么多的赞成票,因为我在回答的顶部提到了这一点
【解决方案2】:

错误Event onerror 处理程序接收到is a simple event not containing such information

如果要求用户代理使 WebSocket 连接失败或 WebSocket 连接因偏见而关闭,则在 WebSocket 对象上触发一个名为 error 的简单事件。

您可能会更幸运地监听close 事件,它是一个CloseEvent,并且确实有一个CloseEvent.code 属性,其中包含根据RFC 6455 11.7 的数字代码和一个CloseEvent.reason 字符串属性。

但请注意,CloseEvent.code(和CloseEvent.reasonare limited 的方式可以避免网络探测和其他安全问题。

【讨论】:

  • 我用的是Chrome,原因是"",奇怪的是F12开发者工具提供了更多的信息。
  • @Dr.YSG 这一点也不奇怪。开发人员工具中的错误消息是针对浏览器用户的,因此它可以包含敏感信息。 OTOH 一些随机的 JavaScript 代码通常不受信任。否则,您可能会编写蠕虫(如端口扫描程序或 DDoS 脚本)并通过一些随机广告网络传播它。
【解决方案3】:

对于那些不顾一切的人来说可能是愚蠢的解决方法:返回一个状态代码。通过访问处理程序接收的参数的message 属性,可以从onerror 事件处理程序查看状态代码。我建议选择 440s——似乎是免费的房地产。

“意外的服务器响应:440”

一点点正则表达式就可以解决问题:

const socket = new WebSocket(/* yuh */);

socket.onerror = e => {
  const errorCode = e.message.match(/\d{3}/)[0];
  // errorCode = '440'
  // make your own rudimentary standard for error codes and handle them accordingly
};

可能在紧要关头有用,但不要因为任何不可预见的后果而向我哭泣。

【讨论】:

  • 我的 onerror 函数()它没有你提到的“消息”属性。
  • @PedroJoaquín 我在节点 1.14 上使用 ws。你在什么环境下?
  • 纯javascript
  • 浏览器中的纯 JavaScript 还是节点的一部分?我应该更清楚;我关心您使用的是什么特定的运行时。 @PedroJoaquín
  • 对不起,我刚开始不久,我在 JavaWeb 应用程序中使用浏览器 javascript
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多