Html Websocket onerror - 如何阅读错误描述?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18803971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Websocket onerror - how to read error description?
提问by Sinisa
I've been developing browser-based multi player game for a while now and I've been testing different ports accessibility in various environment (client's office, public wifi etc.). All is going quite well, except one thing: I can't figure out is how to read error no. or description when onerror event is received.
我开发基于浏览器的多人游戏已经有一段时间了,我一直在测试各种环境(客户办公室、公共 wifi 等)中不同端口的可访问性。一切都很顺利,除了一件事:我不知道如何读取错误号。或收到 onerror 事件时的描述。
Client websocket is done in javascript.
客户端 websocket 是在 javascript 中完成的。
For example:
例如:
// Init of websocket
websocket = new WebSocket(wsUri);
websocket.onerror = OnSocketError;
...etc...
// Handler for onerror:
function OnSocketError(ev)
{
output("Socket error: " + ev.data);
}
'output' is just some utility function that writes into a div.
“输出”只是一些写入 div 的实用函数。
What I am getting is 'undefined' for ev.data. Always. And I've been googling around but it seems there's no specs on what params this event has and how to properly read it.
我得到的是 ev.data 的“未定义”。总是。我一直在谷歌搜索,但似乎没有关于此事件具有哪些参数以及如何正确读取它的规范。
Any help is appreciated!
任何帮助表示赞赏!
采纳答案by nmaier
The error Event
the onerror
handler receives is a simple event not containing such information:
该错误Event
的onerror
处理程序接收不含有这种信息的简单事件:
If the user agent was required to fail the WebSocket connection or the WebSocket connection is closed with prejudice, fire a simple event named error at the WebSocket object.
如果用户代理需要使 WebSocket 连接失败或 WebSocket 连接因偏见而关闭,则在 WebSocket 对象上触发一个名为 error 的简单事件。
You may have better luck listening for the close
event, which is a CloseEvent
and indeed has a CloseEvent.code
property containing a numerical code according to RFC 6455 11.7and a CloseEvent.reason
string property.
您可能有更好的运气侦听close
事件,这是一个CloseEvent
并且确实具有CloseEvent.code
包含根据RFC 6455 11.7的数字代码和CloseEvent.reason
字符串属性的属性。
Please note however, that CloseEvent.code
(and CloseEvent.reason
) are limitedin such a way that network probing and other security issues are avoided.
但是请注意,CloseEvent.code
( 和CloseEvent.reason
)受到限制,可以避免网络探测和其他安全问题。
回答by Phylliida
Alongside nmaier's answer, as he said you'll always receive code 1006. However, if you were to somehow theoretically receive other codes, here is code to display the results (via RFC6455).
除了 nmaier 的回答,正如他所说,您将始终收到代码 1006。但是,如果您在理论上以某种方式接收其他代码,这里是显示结果的代码(通过RFC6455)。
you will almost never get these codes in practice so this code is pretty much pointless
在实践中你几乎永远不会得到这些代码,所以这段代码几乎毫无意义
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();