shanewholloway/js-u8-mqtt

Autoreconnect option reopens WebSocket after intentional disconnect() after two events

Opened this issue · 0 comments

Hi, and first of all, thank you for your work.

While working with your library I noticed an interesting behavior on disconnects which I am not entirely sure whether this intended or a bug.

After the client has been initialized with a WebSocket URI, triggering a mqttClient.disconnect() will actually result in two subsequent on_disconnect events, with the first event having the flag intentional = true and the second event having the flag intentional = false.

image

I have created a codepen to reproduce this: https://codepen.io/sluger/pen/oNrpLRJ It is based on your web-cdn.html demo

The client automatically connects to wss://test.mosquitto.org:8081. Just press the "Disconnect" button to trigger the disconnect method and check the network tab filtering for WebSocket connections. Also, please find the code block where I override the on_disconnect handler to print out the intentional flag which is commented out to demonstrate the reopening of the WebSocket.

Removing the autoreconnect option will have the client not reconnect to the WS again. However, it would be convenient to make use of the reconnect automatism for real connection interrupts while being able to close the connection of a WebSocket on purpose.

Tested with Microsoft Edge - Version 127.0.2651.98 (Official build) (arm64)

Thanks again
Stefan