diff --git a/lib/tunnelbroker/tunnelbroker-context.js b/lib/tunnelbroker/tunnelbroker-context.js --- a/lib/tunnelbroker/tunnelbroker-context.js +++ b/lib/tunnelbroker/tunnelbroker-context.js @@ -138,14 +138,18 @@ // This lasts between socket.close() and socket.onclose() React.useEffect(() => { // when initMessage changes, we need to close the socket and open a new one - if ((!initMessage || initMessageChanged) && isSocketActive) { + if ( + (!initMessage || initMessageChanged) && + isSocketActive && + socket.current + ) { socket.current?.close(); return; } // when we're already connected (or pending disconnection), // or there's no init message to start with, we don't need to do anything - if (connected || !initMessage) { + if (connected || !initMessage || socket.current) { return; } @@ -159,6 +163,7 @@ // this triggers the effect hook again and reconnect setConnected(false); onClose?.(); + socket.current = null; console.log('Connection to Tunnelbroker closed'); }; tunnelbrokerSocket.onerror = e => {