File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
livekit-android-sdk/src/main/java/io/livekit/android Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import io.livekit.android.room.Room
66import io.livekit.android.room.RoomListener
77import io.livekit.android.util.LKLog
88import io.livekit.android.util.LoggingLevel
9+ import io.livekit.android.util.executeSuspendWithRetry
910import timber.log.Timber
1011
1112class LiveKit {
@@ -59,19 +60,23 @@ class LiveKit {
5960 * Connect to a LiveKit room
6061 * @param url URL to LiveKit server (i.e. ws://mylivekitdeploy.io)
6162 * @param listener Listener to Room events. LiveKit interactions take place with these callbacks
63+ * @param maxConnectRetry Int to set max connect retry.
6264 */
6365 suspend fun connect (
6466 appContext : Context ,
6567 url : String ,
6668 token : String ,
6769 options : ConnectOptions = ConnectOptions (),
6870 roomOptions : RoomOptions = RoomOptions (),
69- listener : RoomListener ? = null
71+ listener : RoomListener ? = null ,
72+ maxConnectRetry : Int = 0
7073 ): Room {
7174 val room = create(appContext, roomOptions)
7275
7376 room.listener = listener
74- room.connect(url, token, options)
77+ executeSuspendWithRetry(maxConnectRetry){
78+ room.connect(url, token, options)
79+ }
7580 return room
7681 }
7782
Original file line number Diff line number Diff line change 1+ package io.livekit.android.util
2+
3+ import kotlinx.coroutines.delay
4+
5+ suspend fun <T >executeSuspendWithRetry (maxRetries : Int , suspendFunction : suspend () -> T ): T {
6+ var currentAttempt = 0
7+ var lastException: Exception ? = null
8+
9+ while (currentAttempt <= maxRetries) {
10+ try {
11+ return suspendFunction()
12+ } catch (e: Exception ) {
13+ LKLog .i {" connection number $currentAttempt failed with $e " }
14+ // Store the last exception for error logging
15+ lastException = e
16+ currentAttempt++
17+
18+ // Delay before retrying
19+ delay(1_000L * currentAttempt)
20+ }
21+ }
22+
23+ throw lastException ? : RuntimeException (" Max retries exceeded" )
24+ }
You can’t perform that action at this time.
0 commit comments