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 @@ -125,14 +125,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; } @@ -145,6 +149,7 @@ tunnelbrokerSocket.onclose = () => { // this triggers the effect hook again and reconnect setConnected(false); + socket.current = null; console.log('Connection to Tunnelbroker closed'); }; tunnelbrokerSocket.onerror = e => {