lukeed / sockette

The cutest little WebSocket wrapper! 🧦
MIT License
2.45k stars 81 forks source link

Increment timeout #64

Closed allengordon011 closed 3 years ago

allengordon011 commented 3 years ago

Can I modify the timeout in onreconnect() -- for example, to increase the delay after 10 attempts to reconnect? I do not see the timeout variable on the event.target.

allengordon011 commented 3 years ago

I've successfully modified the timeout by adding the following variables in the constructor() and incrementing in onreconnect(), but now I realize the timeout is being ignored... It seems the Websocket is closing itself after 60 seconds -- because of the WS idle timeout? In that case, what use is the timeout in Sockette?

  this.reconnectCount = 0;
  this.timeout = 5e3;
lukeed commented 3 years ago

If you look at the source, the timeout you pass is never attached to the instance or the WebSocket itself.

If you want to adjust the Sockette settings, you must save the config to a options variable and then invoke new instances with your updated value(s)

let ctx, options = {
  maxAttempts: 1,
  timeout: 1e3,
  // ...
};

options.onmaximum = ev => {
  options.timeout += 1e3; // add 1s
  ctx = new Sockette('...', options);
}

Note: Untested

The idea being that you manually increment the timeout, and rely on Sockette to tell you "wanted to reconnect, but exceeded my maxAttempts value" -- this means that you'd need some other flag/counter to determine when to give up retrying.

Hope that helps!