Skip to content

Commit

Permalink
PWN-843 - reconnect socket on connection true (#2215)
Browse files Browse the repository at this point in the history
  • Loading branch information
gslevinkov authored Feb 23, 2024
1 parent 697f1bf commit 68b57ae
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import org.p2p.wallet.jupiter.ui.main.JupiterSwapFragment
import org.p2p.wallet.settings.ui.settings.SettingsFragment
import org.p2p.wallet.tokenservice.TokenServiceCoordinator
import org.p2p.wallet.tokenservice.UserTokensState
import org.p2p.wallet.updates.SocketUpdatesManager

class MainContainerPresenter(
private val deeplinksManager: AppDeeplinksManager,
private val connectionManager: ConnectionManager,
private val tokenServiceCoordinator: TokenServiceCoordinator,
private val metadataInteractor: MetadataInteractor,
private val walletStrigaInteractor: WalletStrigaInteractor,
private val socketUpdatesManager: SocketUpdatesManager,
private val balanceMapper: WalletBalanceMapper,
private val mainScreenAnalytics: MainScreenAnalytics,
) : BasePresenter<MainContainerContract.View>(), MainContainerContract.Presenter {
Expand Down Expand Up @@ -102,7 +104,14 @@ class MainContainerPresenter(
private fun observeInternetState() {
connectionManager.connectionStatus
.onEach { isConnected ->
if (!isConnected) view?.showUiKitSnackBar(messageResId = R.string.error_no_internet_message)
if (!isConnected) {
view?.showUiKitSnackBar(messageResId = R.string.error_no_internet_message)
socketUpdatesManager.stop()
} else {
if (!socketUpdatesManager.isStarted()) {
socketUpdatesManager.restart()
}
}
}
.launchIn(this)
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/org/p2p/wallet/updates/SocketUpdatesManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import org.p2p.core.BuildConfig.rpcPoolApiKey
import org.p2p.core.common.di.AppScope
import org.p2p.core.network.ConnectionManager
import org.p2p.core.network.environment.NetworkEnvironment
import org.p2p.core.network.environment.NetworkEnvironmentManager
import org.p2p.solanaj.model.types.RpcMapRequest
import org.p2p.solanaj.model.types.RpcRequest
import org.p2p.solanaj.ws.SocketClientCreateResult
Expand All @@ -26,10 +29,7 @@ import org.p2p.solanaj.ws.SubscriptionEventListener
import org.p2p.solanaj.ws.SubscriptionSocketClient
import org.p2p.solanaj.ws.SubscriptionSocketClientFactory
import org.p2p.solanaj.ws.impl.SocketClientException
import org.p2p.core.common.di.AppScope
import org.p2p.wallet.common.feature_toggles.toggles.remote.SocketSubscriptionsFeatureToggle
import org.p2p.core.network.environment.NetworkEnvironment
import org.p2p.core.network.environment.NetworkEnvironmentManager

private const val DELAY_MS = 5000L

Expand Down Expand Up @@ -149,8 +149,10 @@ class SocketUpdatesManager(
}
}

override fun onClosed(code: Int, message: String) {
override fun onClientClosed(code: Int, message: String) {
Timber.tag(TAG).i("Event updates connection is closed: $message")
isStarted = false
state = SocketState.DISCONNECTED
}

private suspend fun startActual() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.p2p.solanaj.ws

import java.lang.Exception

interface SocketStateListener {
fun onConnected()
fun onWebSocketPong()
fun onFailed(exception: Exception)
fun onClosed(code: Int, message: String)
fun onClientClosed(code: Int, message: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal class SubscriptionWebSocketClient internal constructor(

override fun onClose(code: Int, reason: String, remote: Boolean) {
val closedFrom = if (remote) "remote peer" else "us"
stateListener.onClosed(
stateListener.onClientClosed(
code = code,
message = "Connection closed by $closedFrom Code: $code Reason: $reason"
)
Expand Down

0 comments on commit 68b57ae

Please sign in to comment.