Websocket的onerror – 如何阅读错误描述?

我一直在开发基于浏览器的多玩家游戏,我已经在不同的环境(客户端办公室,公共WiFi等)testing了不同的端口可访问性。 一切进展顺利,除了一件事:我不知道是如何阅读错误号。 或接收到错误事件时的描述。

客户端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的“未定义”。 总是。 我一直在search,但似乎没有关于这个事件有什么参数和如何正确阅读的规格。

任何帮助表示赞赏!

onerror处理程序收到的错误Event 是一个不包含这些信息的简单事件 :

如果用户代理被要求失败的WebSocket连接或WebSocket连接closures偏见,在WebSocket对象触发一个简单的事件名为错误。

你可能有更好的运气来监听close事件,它是一个CloseEvent事件,并且确实有一个CloseEvent.code属性,它包含一个根据RFC 6455 11.7的数字代码和一个CloseEvent.reasonstring属性。

但请注意, CloseEvent.code (和CloseEvent.reason ) 是有限的,以避免networking探测和其他安全问题。

除了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 (eg, 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, eg, 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 (eg, 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 (eg, 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/