OokTech/TW5-Bob

Update handling of broken websocket connectitons

Opened this issue · 0 comments

There is a recommended method for handling the ping pong messages as described here #172 (comment)

From the research @joshuafontany did copied from the above link:

Right now Bob is using the heartbeat Pongs to setup a timer for a Ping. Then a series of timers re-send Pings and test for if($tw.connections[0].socket.readyState !== 1). I have been reading the ws repo on github, and they recommend a different logic flow in their "Pong handler". On a successful "Pong" it destroys a waiting pingTimeout timer, then sets it again to wait the Heartbeat time + a "normal latency expectation". If another Pong doesn't happen in time, it calls terminate() on the websocket.

function heartbeat() {
  clearTimeout(this.pingTimeout);

  // Use `WebSocket#terminate()`, which immediately destroys the connection,
  // instead of `WebSocket#close()`, which waits for the close timer.
  // Delay should be equal to the interval at which your server
  // sends out pings plus a conservative assumption of the latency.
  this.pingTimeout = setTimeout(() => {
    this.terminate();
  }, 30000 + 1000);
}

Then, we can use the ws.onclose handler to attempt reconnects (there is a good method to call them at increasing intervals), and then once reconnected Bob can resend any missed messages back and forth. This will allow an auto-reconnect method, and we only have to show the manual Reconnect button if the largest Reconnect interval fails.