Websocket onerror-如何读取错误描述?


79

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

客户端网络套接字是用javascript完成的。

例如:

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

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

“输出”只是一些写入div的实用程序函数。

我得到的是ev.data的“未定义”。总是。我一直在搜寻,但是似乎没有关于此事件具有什么参数以及如何正确读取它的规范。

任何帮助表示赞赏!


2
请注意,这是设计使您无法从websocket中获得有用的错误信息:stackoverflow.com/a/31003057/771768
Carl Walsh

Answers:


75

该错误Eventonerror处理程序接收不含有这种信息的简单事件

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

您可能会更好地监听该close事件,该事件CloseEvent确实是a,并且确实具有CloseEvent.code根据RFC 6455 11.7包含数字代码的属性和CloseEvent.reasonstring属性。

但是请注意,CloseEvent.code(和CloseEvent.reason受到限制,可以避免网络探测和其他安全问题。


5
我正在使用Chrome,原因是“”,奇怪的是F12开发人员工具提供了更多信息。
YSG博士,2014年

4
@ Dr.YSG这一点都不奇怪。开发人员工具中的错误消息是针对浏览器用户的,因此它可能包含敏感信息。OTOH一些随机的JavaScript代码通常是不可信的。否则,您可以编写蠕虫(例如端口扫描程序或DDoS脚本),然后通过某个随机广告网络进行传播。
jpc 2015年

88

正如nmaier的回答,正如他所说,您将始终收到代码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 http://tools.ietf.org/html/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 [http://tools.ietf.org/html/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/


2
这些事件代码无效,您通常只能接收1006代码。
匿名

2
是的,除非他们在某种奇怪的情况下可以接收更多代码,否则我的答案是假设的,我不确定为什么它会有这么多的投票,因为我在答案的最前面提到了
Phylliida
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.