lukeed / sockette

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

onmaximum not triggering #23

Closed GMchris closed 5 years ago

GMchris commented 6 years ago

Am I misunderstanding this method or is it not working?

socket = new Sockette(action.url, {
        timeout: 5000,
        maxAttempts: 2,
        onopen: onOpen(store),
        onclose: onClose(store),
        onmessage: onMessage(store),
        onmaximum: onMaximum(store),
        onreconnect: onReconnect(store),
        onerror: noop,
      });

I have this snippet setup. Everything works as expected. When the WS server is down, Sockette tries to connect twice, before giving up, however the onmaximum callback is never triggered.

lukeed commented 6 years ago

Hey, what if you console.log out the event listeners?

I've done that just now to re-verify & the onmaximum callback is triggered correctly.

xuzeshen commented 6 years ago

$.onopen = e => { num=0; (opts.onopen || noop)(e); }; There should't do num = 0, otherwise, num will lose the original purpose as a global counter @lukeed @GMchris

lukeed commented 6 years ago

@DarcyXf It isn't meant to be global. The onopen callback is only called when the connection has been successfully established. We reset the num counter because we want to allow maxAttempts for every new reconnection.

For example, with a maxAttempts of 3 and without resetting every successful connection, this would be the series of events:

new Sockette({ maxAttempts:3, timeout:1000 })
//=> 0

*server shutdown*

*wait 1s*
*attempt*
//=> 1

*wait 1s*
*attempt*
//=> 2

*RECONNECTED*
//=> 2

*server shutdown*

*wait 1s*
*attempt*
//=> 3

*GIVE UP FOREVER*

Instead, we'd want try again 3 times per connection failure.

lukeed commented 5 years ago

If you have any more insight for this @GMchris please let me know – I know it's been a while but it'd be very helpful 🙇

Closing in the meantime as I'm still unable to reproduce this.