Close connection after timeout #36
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Listen for a heartbeat in form of WebSocket Pings (see RFC 6455) and close the connection if none are received within a specified timeframe.
This solves the following problem:
If the network connection gets interrupted long enough the connection might time out on the server side but this module still shows the connection as active indefinitely. Which means in order to reconnect one has to manually disable and reenable the connection within Companion.
With this option enabled the connection will eventually time out on both sides and the module can automatically establish a new connection (if the reconnect option is enabled).
I made this function optional and the timeout configurable.
Apparently a common interval for sending those pings is 20 seconds (examples: WebSocket-Node, Python websockets) so 30 seconds seem like a reasonable default.
The timeout starts only after the first ping is received so if a server doesn't send any pings the connection will not get closed even if the option is enabled. However I still decided against enabling it by default because if a server does send pings but at an interval longer than specified (for example 60 seconds) the connection will close after the timeout despite having a stable network connection.
The implementation is based on the example code in Node.js WebSocket library.