Websocket onerror – come leggere la descrizione dell’errore?

Sto sviluppando un gioco multi-player basato su browser per un po ‘di tempo e sto testando l’accessibilità di diverse porte in vari ambienti (ufficio del cliente, wifi pubblico, ecc.). Tutto sta andando abbastanza bene, tranne una cosa: non riesco a capire come leggere l’errore no. o descrizione quando viene ricevuto un evento onerror.

Websocket del client è fatto in javascript.

Per esempio:

// Init of websocket websocket = new WebSocket(wsUri); websocket.onerror = OnSocketError; ...etc... // Handler for onerror: function OnSocketError(ev) { output("Socket error: " + ev.data); } 

‘output’ è solo una funzione di utilità che scrive in un div.

Quello che sto ottenendo è “indefinito” per ev.data. Sempre. E ho cercato su google, ma sembra che non ci siano specifiche su quali parametri questo evento abbia e su come leggerlo correttamente.

Qualsiasi aiuto è apprezzato!

L’ Event errore onerror gestore onerror è un evento semplice che non contiene tali informazioni :

Se è stato richiesto all’agente utente di fallire la connessione WebSocket o la connessione WebSocket è chiusa con pregiudizio, triggersre un evento semplice denominato errore sull’object WebSocket.

Potresti avere una migliore fortuna nell’ascoltare l’evento close , che è un CloseEvent e in effetti possiede una proprietà CloseEvent.code contiene un codice numerico secondo RFC 6455 11.7 e una proprietà string CloseEvent.reason .

Si prega di notare, tuttavia, che CloseEvent.code (e CloseEvent.reason ) sono limitati in modo tale da evitare il CloseEvent.reason rete e altri problemi di sicurezza.

Accanto alla risposta di nmaier, come ha detto , riceverai sempre il codice 1006 . Tuttavia, se in qualche modo dovessi ricevere in teoria altri codici ( ma questo non accadrà quasi mai in pratica, quindi questo codice è praticamente inutile ), ecco il codice per visualizzare i risultati (tramite RFC6455 ).

 var websocket; if ("WebSocket" in window) { websocket = new WebSocket("ws://yourDomainNameHere.org/"); websocket.onopen = function (event) { $("#thingsThatHappened").html($("#thingsThatHappened").html() + "
" + "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.
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() + "
" + "The connection was closed for reason: " + reason); }; websocket.onmessage = function (event) { $("#thingsThatHappened").html($("#thingsThatHappened").html() + "
" + "New message arrived: " + event.data); }; websocket.onerror = function (event) { $("#thingsThatHappened").html($("#thingsThatHappened").html() + "
" + "There was an error with your websocket."); }; } else { alert("Websocket is not supported by your browser"); return; } websocket.send("Yo wazzup"); websocket.close();

Vedi http://jsfiddle.net/gr0bhrqr/