Search code examples
javascriptwebsocket

Websocket onerror event gives no information


I am developing a client and server application where a websocket will be used to maintain communications. I create a websocket:

function DebugMsg(msg) {
    var msgdump = document.getElementById("msgdump");
    if (msgdump == null || typeof msgdump === "undefined") {
        return;
    }
    var strContent;    
    if (typeof msg == "string" && msg.length > 0) {
        strContent = msg;
    }
    if (typeof msg == "object" && typeof msg.message == "string") {
        strContent = msg.message;
    }
    if (typeof strContent == "string" && strContent.length > 0) {
        strContent = (new Date()).toUTCString() + ", " + strContent + "..."; 
        msgdump.innerHTML = strContent + "\r\n" + msgdump.innerHTML;
    }
}
function InstallServiceTimer(intInterval) {    
    if (intServiceInterval != intInterval) {
        tmrService = setInterval(ServiceRequests, intInterval);
        if (tmrService != null && typeof tmrService == "object") {       
            intServiceInterval = intInterval;
        }
    }
    if (!(tmrService == null || typeof tmrService === "undefined")) {
        DebugMsg("tmrService set-up, interval: " + intInterval + "ms");
    }
}
//Function called when service request timer expires, no arguments
function ServiceRequests() { 
    if (!(typeof webSocket == "object"
       && typeof webSocket.readyState == "number" 
              && webSocket.readyState == 1)) {
        return;
    }
    objServiceInfo[cstrServiceInfoTagRequests]++;
    var intServiceReqRespDiff = objServiceInfo[cstrServiceInfoTagRequests] 
                                - objServiceInfo[cstrServiceInfoTagResponses];
    if (intServiceReqRespDiff > cintServiceNoResponses
        && intServiceInterval != cintServiceRequestAltInterval) {
    //Stop the existing timer
        clearInterval(tmrService);
    //Reset request and response counters
        objServiceInfo[cstrServiceInfoTagRequests] = 
        objServiceInfo[cstrServiceInfoTagResponses] = 0;
    //Create a new timer with the slower interval
        InstallServiceTimer(cintServiceRequestAltInterval);
        return;
    }
    DebugMsg("Sending: \"" + cstrSendMsg + "\", to: " + cstrServerSocketIP);
    webSocket.send(cstrSendMsg);
}
function CreateWebSocket() {
    try {
    //Set-up timer to monitor webSocket object
        if (typeof tmrWebSocketMonitor == "undefined") {
            tmrWebSocketMonitor = setInterval(function() {
                if (webSocket == null ) {
    //Do nothing webSocket hasn't been created yet
                    return;
                }
                if (!(typeof webSocket == "object"
                   && typeof webSocket.readyState == "number" 
                          && webSocket.readyState == 1)) {
                    webSocket.close();
                }
            }, 5000);
        }
        webSocket = new WebSocket(cstrServerSocketIP);    
        webSocket.onclose = (event) => {
            var strReason = "";    
            if (typeof event == "object") {
                if (typeof event.code == "number") {
                    strReason += "code[" + event.code + "]";
                    var strError = cobjWebSocketErrors[event.code];
                    if (typeof strError == "string") {
                        strReason += ":" + strError;
                    }
                }
                if (typeof event.reason == "string" && event.reason.length > 0) {
                    if (strReason.length > 0) {
                        strReason += ", ";    
                    }
                    strReason += "reason:\"" + event.reason + "\"";
                }
            }
            DebugMsg("webSocket.onclose " + strReason);    
        };    
        webSocket.onerror = (event) => {
            DebugMsg("webSocket.onerror" 
                + ((typeof event == "object"
                && typeof event.data == "string") ? ":" + event.data : ""));
        };
        webSocket.onmessage = (event) => { 
            DebugMsg("webSocket.onmessage"
                + ((typeof event == "object"
                    && typeof event.data == "string") ? ":" + event.data : ""));
        };
        webSocket.onopen = () => {
            DebugMsg("webSocket.onopen");
        };
        //Set-up timer to send requests for data updates
        InstallServiceTimer(cintServiceRequestInterval);
    } catch (e) {
        DebugMsg(e);
    }
}

I'm using Chrome as the browser and the Developer Tools within Chrome to help debug. I can see when the onerror event occurs and there is nothing useful in the passed event that I can see that suggests what the error is, no code, no message. Can anyone shed any light on how I can track what the cause of this is?


Solution

  • The onerror event in WebSocket is a simple event and does not provide detailed error information. To identify the error, you can use the onclose event and analyze the code returned within the onclose event