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.