File tree 2 files changed +31
-2
lines changed
livekit-android-sdk/src/main/java/io/livekit/android
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
6
6
import io.livekit.android.room.RoomListener
7
7
import io.livekit.android.util.LKLog
8
8
import io.livekit.android.util.LoggingLevel
9
+ import io.livekit.android.util.executeSuspendWithRetry
9
10
import timber.log.Timber
10
11
11
12
class LiveKit {
@@ -59,19 +60,23 @@ class LiveKit {
59
60
* Connect to a LiveKit room
60
61
* @param url URL to LiveKit server (i.e. ws://mylivekitdeploy.io)
61
62
* @param listener Listener to Room events. LiveKit interactions take place with these callbacks
63
+ * @param maxConnectRetry Int to set max connect retry.
62
64
*/
63
65
suspend fun connect (
64
66
appContext : Context ,
65
67
url : String ,
66
68
token : String ,
67
69
options : ConnectOptions = ConnectOptions (),
68
70
roomOptions : RoomOptions = RoomOptions (),
69
- listener : RoomListener ? = null
71
+ listener : RoomListener ? = null ,
72
+ maxConnectRetry : Int = 0
70
73
): Room {
71
74
val room = create(appContext, roomOptions)
72
75
73
76
room.listener = listener
74
- room.connect(url, token, options)
77
+ executeSuspendWithRetry(maxConnectRetry){
78
+ room.connect(url, token, options)
79
+ }
75
80
return room
76
81
}
77
82
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