diff --git a/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt b/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt index 64c92ab7..3849b5e5 100644 --- a/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt +++ b/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt @@ -105,6 +105,10 @@ internal class Connection( on(webSocketOpen()) { transitionTo(Connected(session = session)) } + on(lifecycleStop()) { + initiateShutdown(it.state) + transitionTo(Disconnecting) + } on() { val backoffDuration = backoffStrategy.backoffDurationMillisAt(retryCount) val timerDisposable = scheduleRetry(backoffDuration) @@ -204,6 +208,13 @@ internal class Connection( } } + private fun Connecting.initiateShutdown(state: Lifecycle.State) { + when (state) { + is Lifecycle.State.Stopped.WithReason -> session.webSocket.close(state.shutdownReason) + Lifecycle.State.Stopped.AndAborted -> session.webSocket.cancel() + } + } + private fun WaitingToRetry.cancelRetry() = timerDisposable.dispose() private fun lifecycleStart() =