Tinder / Scarlet

A Retrofit inspired WebSocket client for Kotlin, Java, and Android
Other
3.2k stars 236 forks source link

Duplicate response from websocket is coming after a channel is unsubscribed and subscribed again #214

Open rohitiiita007 opened 2 years ago

rohitiiita007 commented 2 years ago

I have a use case where I unsubscribe a channel and then subscribe again. But somehow multiple duplicate response is received . Below are my subscribe & unsubscribe methods `

fun subscribeChannel() {
     viewModelScope.launchWithException {
            if (socketRepository.isSocketConnected) {
                socketRepository.subscribeChannel(coinName, ChannelNameConstants.CHANNEL_ORDER_BOOK)
            } else {
                val socketFlow = socketRepository.getWebSocketEvents()
                socketFlow.collect {
                    when (it) {
                        is WebSocket.Event.OnConnectionOpened<*> -> {
                            socketRepository.isSocketConnected = true
                            socketRepository.subscribeChannel(coinName, ChannelNameConstants.CHANNEL_ORDER_BOOK)
                        }
                        is WebSocket.Event.OnConnectionClosed,
                        is WebSocket.Event.OnConnectionFailed -> {
                            socketRepository.isSocketConnected = false
                        }
                    }
                }
            }
        }
}
    fun unsubscribeChannel(coinName: String, channelName: String) {
    socketService.sendUnsubscribe(
        ChannelData(
            channelName = "$channelName ${SubscriptionType.UNSUBSCRIBE.value}",
            data = coinName
        )
    )
}
`