0

I have a web app with 3 websockets to Arduino devices. If the Arduino devices are online, then everything works fine. The problem arises if one of them is not connected to the network when the web app starts the websockets. In this scenario, the onerror() triggers (as it should), but the setInterval() call triggers immediately instead of waiting 30s. This is causing the web browser to run out of resources and crash.

My code:

var websocket1;
var websocket2;
var websocket3;
var sockTimer_1 = null;
var sockTimer_2 = null;
var sockTimer_3 = null;
var sockTimer =   null; // global one
const wsRetry = 30; // number of seconds to wait for retry


function initWebSocket( whichSocket )
{
    console.log( `Trying to open a WebSocket connection ${whichSocket}...` );

    if( whichSocket == 1 )
    {
        websocket1 = new WebSocket( gateway1 );
        websocket1.onopen = function() { onOpen(1); };
        websocket1.onerror = function() { onError(1); };
        websocket1.onclose = function() { onClose(1); };
        websocket1.onmessage = onMessage_1;
    }
    else
    if( whichSocket == 2 )
    {
        websocket2 = new WebSocket( gateway2 );
        websocket2.onopen = function() { onOpen(2); };
        websocket2.onerror = function() { onError(2); };
        websocket2.onclose = function() { onClose(2); };
        websocket2.onmessage = onMessage_2;
    }
    else
    if( whichSocket == 3 )
    {
        websocket3 = new WebSocket( gateway3 );
        websocket3.onopen = function() { onOpen(3); };
        websocket3.onerror = function() { onError(3); };
        websocket3.onclose = function() { onClose(3); };
        websocket3.onmessage = onMessage_3;
    }
}

function onOpen( event )
{
    if( event == 1 )
        clearInterval( sockTimer_1 );
    else
    if( event == 2 )
        clearInterval( sockTimer_2 );
    else
    if( event == 3 )
        clearInterval( sockTimer_3 );

    console.log( 'Connection opened: ' + event );
}

function onError( event )
{
    console.log( 'Connection error - trying again: ' + event );

    if( event == 1 )
    {
        if( sockTimer_1 )
            clearInterval( sockTimer_1 );
        sockTimer_1 = setInterval( initWebSocket( event ), wsRetry * 1000 );
    }
    else
    if( event == 2 )
    {
        if( sockTimer_2 )
            clearInterval( sockTimer_2 );
        sockTimer_2 = setInterval( initWebSocket( event ), wsRetry * 1000 );

    }
    else
    if( event == 3 )
    {
        if( sockTimer_3 )
            clearInterval( sockTimer_3 );
        sockTimer_3 = setTimeout( initWebSocket( event ), wsRetry * 1000 );
    }
}



function onClose( event )
{
    console.log( 'Connection closed - reconnecting: ' + event );
    // sockTimer = setInterval( initWebSocket( event ), wsRetry * 1000 );
    if( event == 3 )
    {
        if( sockTimer_3 )
            clearTimeout( sockTimer_3 );
        sockTimer_3 = setTimeout( initWebSocket( event ), wsRetry * 1000 );
    }
}

Note: I have the onClose() function simplified to just the one that I'm testing with. If errors happen after the websocket is established, it seems to work okay. It's just when trying to establish the connection (and the device isn't there) that the timeouts go haywire.

1 Answer 1

1

With this line, you are actually calling the timeout handler:

sockTimer_1 = setInterval( initWebSocket( event ), wsRetry * 1000 );

should be a function, not a function call.

sockTimer_1 = setInterval( function() {
  initWebSocket( event )
}, wsRetry * 1000 );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.