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.
Client websocket is done in 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.
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.
Any help is appreciated!
The error
Event
theonerror
handler receives is a simple event not containing such information:You may have better luck listening for the
close
event, which is aCloseEvent
and indeed has aCloseEvent.code
property containing a numerical code according to RFC 6455 11.7 and aCloseEvent.reason
string property.Please note however, that
CloseEvent.code
(andCloseEvent.reason
) are limited in such a way that network probing and other security issues are avoided.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).
you will almost never get these codes in practice so this code is pretty much pointless
See http://jsfiddle.net/gr0bhrqr/