FlowFuse / device-agent

An agent to run FlowFuse managed instances of Node-RED on devices
Apache License 2.0
15 stars 8 forks source link

Ensure local ws connections are closed on disconnect #255

Closed knolleary closed 2 months ago

knolleary commented 2 months ago

Fixes https://github.com/FlowFuse/flowfuse/issues/3751

When the device agent tunnel connection is dropped, we weren't closing down the local ws connection to Node-RED - however the platform side does tidy up the corresponding editor->platform connections. This meant that when the tunnel reconnects, the editors create new ws connections which in turn create new ws connections to Node-RED in the agent. This causes duplicate messages to be sent back over the proxy to the connected clients.

This fixes it by ensuring the local ws connections are closed if the tunnel closes.

Whilst debugging, it was also observed that if a tunnel fails to connect, we wouldn't retry the connection; the retry logic was only be applied if a connected socket is dropped unexpectedly.

This fixes that by not treating 'ECONNREFUSED' as a fatal error as it did before.

I've also increased the default reconnect timeout to 1.5s from 0.5s to give the remove end a bit more time to recover, with out any real impact on the user experience.