Attempts to fix ENG-6101.
The problem is that the tunnelbroker connection is not reset when the credentials change, so the
new connection (with new credentials) is established while the old connection is still active.
After a few different approaches, by trial and error, I found this way is the most reliable, in contrast to some complex if-statement conditions.
The flow is as follows:
- Credentials change
- If the connection is active (either waiting for Tunnelbroker Init Response or connected), but not pending disconnection, then disconnect it by calling socket.close().
- The socket.onclose event handler will be called, which will set connected = false.
- The connected hook dependency will trigger the effect hook again to establish a new connection.
A few notes:
- I introduced initMessageChanged because it turned to be the most reliable way of checking it, without unnecessary re-renders.
- I find that relying on socket.current?.readyState is more reliable than checking socket.current != null.
- The four states described in the comments somehow overlap with WebSocket.readyState states, with the exception of "CONNECTING" phase being longer because it waits for the initialization response.