Problem:
My program tries to create websocket and generates an error.
The code is:
let errMsg = "";
try {
socket = new WebSocket(server_ip.value, "chat"); // (*)
}
catch(err) {
// js doesn't come to this branch
Websocketerror = true;
errMsg = "Websocket creation error. Code = " + err.code.toString() +
" Description = _" + err.message + "_";
}
if (websocketError) {
appendMessageText(errMsg, "msg_err");
}
else {
socket.onopen = ws_OnConnectionCreated;
socket.onclose = ws_onConnectionClose;
socket.onerror = ws_OnConnectionError;
}
function ws_onConnectionClose(event) {
let status = event.wasClean ? "ok." : "(aborted).";
appendMessageText("Websocket is closed" + status, "msg_log");
console.log("------> ws_onConnectionClose(event): event =", event);
};
I see an error message in console: “Websocket connection to …ip… failed: Error during WebSocket handshake: Unexpected response code: 499”. It refers to the source line (*)
It occurs when antivirus is active – I guess it blocks websocket-connection and forces returning the code = 499.
My handler function ws_onConnectionClose(event)
was called, but I don’t see – from which variable I can get error message (I have searched it in event
by console.log(event)
) ?
The question: how can I get error’s content (error message, described above) ?
Solution:
Short answer: You can’t.
For security reasons the detailed debug info is not available to JS. You wouldn’t want me to be able to put some javascript on my public website that probes your local network and sends back detailed information about the connection attempts.
That being said, you can still go by event.wasClean
and event.code
to display a generic error. The error codes are defined in RFC 6455, but from my experience it’s almost always code 1006 which is being returned. Also from my experience, the event.reason
property is almost always an empty string.