Idea #7658
Updated by Peter Amstutz over 8 years ago
In order to start having long-running tasks that rely on websockets events to trigger state changes, we need to ensure that the Python websockets client will automatically reconnect when there is an unexpected disconnect. h2. Acceptance criteria * The public interface of EventClient does not change. * Clients that use the public interface of EventClient are automatically get reconnected to websockets. In other words, the @on_event@ callback is called with an uninterrupted stream of events, even if the underlying client has to connect to the websockets server multiple times to provide it. * Existing tests should continue to pass without modifying the public methods called and the results they return. * Add a test that forcibly disconnects the client and asserts that it automatically reconnects and passes in the correct last_log_id. h2. Suggested implementation # The current EventClient class becomes an internal class (_EventClient) # Add a @on_closed()@ callback to _EventClient. # Create a new EventClient class that inherits from object. _EventClient. # EventClient takes @on_event@ in the constructor and has the following public API: @subscribe()@, @unsubscribe()@, @close()@ # EventClient creates an instance of _EventClient and sets callbacks on_event() and on_closed() to itself # When subscribe() and unsubscribe() are called EventClient, record in an array and then forward to _EventClient # When events are called from _EventClient, record the sequence number "id" in last_log_id and forward them to the real on_event() handler. # If close() is called by user, set a flag # If on_closed() event is called on EventClient, but close flag is false, disconnect from old _EventClient object, create a new _EventClient object, and replay the subscriptions with last_log_id set