Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling of broken websocket connectitons #191

Open
inmysocks opened this issue Oct 31, 2022 · 0 comments
Open

Update handling of broken websocket connectitons #191

inmysocks opened this issue Oct 31, 2022 · 0 comments

Comments

@inmysocks
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant