Skip to content

Commit a3c20c0

Browse files
committed
[IDLE-468] 웹소켓 연결 실패시 최대 5번까지 리트라이 하도록 변경
1 parent 15110e0 commit a3c20c0

File tree

6 files changed

+36
-20
lines changed

6 files changed

+36
-20
lines changed

app/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
namespace = "com.idle.care"
1212

1313
defaultConfig {
14-
versionCode = 20
15-
versionName = "1.2.5"
14+
versionCode = 21
15+
versionName = "1.2.6"
1616
targetSdk = 34
1717

1818
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

app/release/app-release.aab

44 MB
Binary file not shown.

core/network/src/main/java/com/idle/network/di/RetrofitModule.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ object RetrofitModule {
6565
fun provideWebSocketOkHttpClient(): OkHttpClient = OkHttpClient.Builder()
6666
.connectTimeout(10, TimeUnit.SECONDS)
6767
.readTimeout(0, TimeUnit.MILLISECONDS)
68+
.apply {
69+
if (BuildConfig.DEBUG) {
70+
val loggingInterceptor = HttpLoggingInterceptor()
71+
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
72+
addInterceptor(loggingInterceptor)
73+
}
74+
}
6875
.build()
6976

7077
@Singleton
@@ -122,4 +129,4 @@ annotation class AuthOkHttpClient
122129

123130
@Qualifier
124131
@Retention(AnnotationRetention.BINARY)
125-
annotation class WebSocketOkHttpClient
132+
annotation class WebSocketOkHttpClient

core/network/src/main/java/com/idle/network/source/websocket/CareWebSocketListener.kt

+1-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ class ChatMessageListener @Inject constructor(private val json: Json) : WebSocke
3636
_chatMessageChannel.value = chatMessageResponse
3737
}
3838

39-
override fun onOpen(webSocket: WebSocket, response: Response) {
40-
super.onOpen(webSocket, response)
41-
}
42-
4339
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
4440
super.onFailure(webSocket, t, response)
4541
}
46-
}
42+
}

core/network/src/main/java/com/idle/network/source/websocket/WebSocketDataSource.kt

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.idle.network.source.websocket
22

3-
import com.idle.domain.model.error.ErrorHelper
43
import com.idle.network.BuildConfig
54
import com.idle.network.di.WebSocketOkHttpClient
65
import com.idle.network.model.chatting.ChatMessageResponse
@@ -15,9 +14,9 @@ import javax.inject.Singleton
1514
class WebSocketDataSource @Inject constructor(
1615
@WebSocketOkHttpClient private val client: OkHttpClient,
1716
private val chatMessageListener: ChatMessageListener,
18-
private val errorHelper: ErrorHelper,
1917
) {
2018
private lateinit var chatMessageWebSocket: WebSocket
19+
private var connectionAttempts = 0
2120

2221
fun connectWebSocket(): Result<Unit> {
2322
return try {
@@ -26,9 +25,15 @@ class WebSocketDataSource @Inject constructor(
2625
.build()
2726

2827
chatMessageWebSocket = client.newWebSocket(chatMessageRequest, chatMessageListener)
28+
connectionAttempts = 0
2929
Result.success(Unit)
3030
} catch (e: Exception) {
31-
Result.failure(e)
31+
if (connectionAttempts < MAX_RETRY_ATTEMPTS) {
32+
connectionAttempts++
33+
connectWebSocket()
34+
} else {
35+
Result.failure(e)
36+
}
3237
}
3338
}
3439

@@ -44,4 +49,8 @@ class WebSocketDataSource @Inject constructor(
4449
}
4550

4651
fun getChatMessageFlow(): StateFlow<ChatMessageResponse?> = chatMessageListener.chatMessageFlow
47-
}
52+
53+
companion object {
54+
private const val MAX_RETRY_ATTEMPTS = 5
55+
}
56+
}

presentation/src/main/java/com/idle/presentation/MainViewModel.kt

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.idle.presentation
22

3+
import android.util.Log
34
import androidx.lifecycle.ViewModel
45
import androidx.lifecycle.viewModelScope
56
import com.idle.analytics.error.ErrorLoggingHelper
@@ -16,6 +17,8 @@ import com.idle.domain.model.profile.CenterManagerAccountStatus
1617
import com.idle.domain.repositorry.jobposting.JobPostingRepository
1718
import com.idle.domain.usecase.auth.GetAccessTokenUseCase
1819
import com.idle.domain.usecase.auth.GetUserTypeUseCase
20+
import com.idle.domain.usecase.chatting.ConnectWebSocketUseCase
21+
import com.idle.domain.usecase.chatting.DisconnectWebSocketUseCase
1922
import com.idle.domain.usecase.config.GetForceUpdateInfoUseCase
2023
import com.idle.domain.usecase.notification.ReadNotificationUseCase
2124
import com.idle.domain.usecase.profile.GetCenterStatusUseCase
@@ -47,8 +50,8 @@ class MainViewModel @Inject constructor(
4750
private val getCenterStatusUseCase: GetCenterStatusUseCase,
4851
private val readNotificationUseCase: ReadNotificationUseCase,
4952
private val jobPostingRepository: JobPostingRepository,
50-
// private val connectWebSocketUseCase: ConnectWebSocketUseCase,
51-
// private val disconnectWebSocketUseCase: DisconnectWebSocketUseCase,
53+
private val connectWebSocketUseCase: ConnectWebSocketUseCase,
54+
private val disconnectWebSocketUseCase: DisconnectWebSocketUseCase,
5255
private val errorHelper: ErrorHelper,
5356
private val eventHelper: EventHelper,
5457
val errorLoggingHelper: ErrorLoggingHelper,
@@ -71,15 +74,16 @@ class MainViewModel @Inject constructor(
7174
}
7275

7376
internal fun connectWebSocket() = viewModelScope.launch {
74-
// Log.d("test", "웹소켓 연결")
75-
// connectWebSocketUseCase().onSuccess { }
76-
// .onFailure { }
77+
Log.d("test", "웹소켓 연결")
78+
connectWebSocketUseCase().onFailure {
79+
Log.d("test", it.stackTraceToString())
80+
}
7781
}
7882

7983
internal fun disconnectWebSocket() = viewModelScope.launch {
80-
// Log.d("test", "웹소켓 연결해제")
81-
// disconnectWebSocketUseCase().onSuccess { }
82-
// .onFailure { }
84+
Log.d("test", "웹소켓 연결해제")
85+
disconnectWebSocketUseCase().onSuccess { }
86+
.onFailure { }
8387
}
8488

8589
internal fun setNavigationMenuType(navigationMenuType: NavigationMenuType) {

0 commit comments

Comments
 (0)