limit reconnecting to 1 minute max.
Also fix bug where always did full reconnect
正在显示
2 个修改的文件
包含
17 行增加
和
8 行删除
| @@ -18,6 +18,7 @@ import livekit.LivekitRtc | @@ -18,6 +18,7 @@ import livekit.LivekitRtc | ||
| 18 | import org.webrtc.* | 18 | import org.webrtc.* |
| 19 | import java.net.ConnectException | 19 | import java.net.ConnectException |
| 20 | import java.nio.ByteBuffer | 20 | import java.nio.ByteBuffer |
| 21 | +import java.util.concurrent.TimeUnit | ||
| 21 | import javax.inject.Inject | 22 | import javax.inject.Inject |
| 22 | import javax.inject.Named | 23 | import javax.inject.Named |
| 23 | import javax.inject.Singleton | 24 | import javax.inject.Singleton |
| @@ -324,17 +325,18 @@ internal constructor( | @@ -324,17 +325,18 @@ internal constructor( | ||
| 324 | connectionState = ConnectionState.RECONNECTING | 325 | connectionState = ConnectionState.RECONNECTING |
| 325 | listener?.onEngineReconnecting() | 326 | listener?.onEngineReconnecting() |
| 326 | 327 | ||
| 327 | - for (wsRetries in 0 until MAX_SIGNAL_RETRIES) { | ||
| 328 | - var startDelay = wsRetries.toLong() * wsRetries * 500 | 328 | + val reconnectStartTime = SystemClock.elapsedRealtime() |
| 329 | + for (retries in 0 until MAX_RECONNECT_RETRIES) { | ||
| 330 | + var startDelay = retries.toLong() * retries * 500 | ||
| 329 | if (startDelay > 5000) { | 331 | if (startDelay > 5000) { |
| 330 | startDelay = 5000 | 332 | startDelay = 5000 |
| 331 | } | 333 | } |
| 332 | 334 | ||
| 333 | - LKLog.i { "Reconnecting to signal, attempt ${wsRetries + 1}" } | 335 | + LKLog.i { "Reconnecting to signal, attempt ${retries + 1}" } |
| 334 | delay(startDelay) | 336 | delay(startDelay) |
| 335 | 337 | ||
| 336 | // full reconnect after first try. | 338 | // full reconnect after first try. |
| 337 | - val isFullReconnect = true | 339 | + val isFullReconnect = retries != 0 |
| 338 | 340 | ||
| 339 | if (isFullReconnect) { | 341 | if (isFullReconnect) { |
| 340 | try { | 342 | try { |
| @@ -383,6 +385,11 @@ internal constructor( | @@ -383,6 +385,11 @@ internal constructor( | ||
| 383 | } | 385 | } |
| 384 | return@launch | 386 | return@launch |
| 385 | } | 387 | } |
| 388 | + | ||
| 389 | + val curReconnectTime = SystemClock.elapsedRealtime() - reconnectStartTime | ||
| 390 | + if(curReconnectTime > MAX_RECONNECT_TIMEOUT){ | ||
| 391 | + break | ||
| 392 | + } | ||
| 386 | } | 393 | } |
| 387 | 394 | ||
| 388 | close() | 395 | close() |
| @@ -518,7 +525,8 @@ internal constructor( | @@ -518,7 +525,8 @@ internal constructor( | ||
| 518 | private const val RELIABLE_DATA_CHANNEL_LABEL = "_reliable" | 525 | private const val RELIABLE_DATA_CHANNEL_LABEL = "_reliable" |
| 519 | private const val LOSSY_DATA_CHANNEL_LABEL = "_lossy" | 526 | private const val LOSSY_DATA_CHANNEL_LABEL = "_lossy" |
| 520 | internal const val MAX_DATA_PACKET_SIZE = 15000 | 527 | internal const val MAX_DATA_PACKET_SIZE = 15000 |
| 521 | - private const val MAX_SIGNAL_RETRIES = 5 | 528 | + private const val MAX_RECONNECT_RETRIES = 10 |
| 529 | + private const val MAX_RECONNECT_TIMEOUT = 60 * 1000 | ||
| 522 | private const val MAX_ICE_CONNECT_TIMEOUT_MS = 20000 | 530 | private const val MAX_ICE_CONNECT_TIMEOUT_MS = 20000 |
| 523 | 531 | ||
| 524 | internal val CONN_CONSTRAINTS = MediaConstraints().apply { | 532 | internal val CONN_CONSTRAINTS = MediaConstraints().apply { |
| @@ -91,14 +91,14 @@ constructor( | @@ -91,14 +91,14 @@ constructor( | ||
| 91 | token: String, | 91 | token: String, |
| 92 | options: ConnectOptions | 92 | options: ConnectOptions |
| 93 | ): Either<LivekitRtc.JoinResponse, Unit> { | 93 | ): Either<LivekitRtc.JoinResponse, Unit> { |
| 94 | + // Clean up any pre-existing connection. | ||
| 95 | + close() | ||
| 96 | + | ||
| 94 | val wsUrlString = "$url/rtc" + createConnectionParams(token, getClientInfo(), options) | 97 | val wsUrlString = "$url/rtc" + createConnectionParams(token, getClientInfo(), options) |
| 95 | isReconnecting = options.reconnect | 98 | isReconnecting = options.reconnect |
| 96 | 99 | ||
| 97 | LKLog.i { "connecting to $wsUrlString" } | 100 | LKLog.i { "connecting to $wsUrlString" } |
| 98 | 101 | ||
| 99 | - // Clean up any pre-existing connection. | ||
| 100 | - close() | ||
| 101 | - | ||
| 102 | coroutineScope = CloseableCoroutineScope(SupervisorJob() + ioDispatcher) | 102 | coroutineScope = CloseableCoroutineScope(SupervisorJob() + ioDispatcher) |
| 103 | lastUrl = wsUrlString | 103 | lastUrl = wsUrlString |
| 104 | 104 | ||
| @@ -518,6 +518,7 @@ constructor( | @@ -518,6 +518,7 @@ constructor( | ||
| 518 | */ | 518 | */ |
| 519 | fun close(code: Int = 1000, reason: String = "Normal Closure") { | 519 | fun close(code: Int = 1000, reason: String = "Normal Closure") { |
| 520 | isConnected = false | 520 | isConnected = false |
| 521 | + isReconnecting = false | ||
| 521 | if (::coroutineScope.isInitialized) { | 522 | if (::coroutineScope.isInitialized) { |
| 522 | coroutineScope.close() | 523 | coroutineScope.close() |
| 523 | } | 524 | } |
-
请 注册 或 登录 后发表评论