Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2025 LiveKit, Inc.
* Copyright 2023-2026 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeout
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand All @@ -62,6 +63,7 @@ import java.util.Date
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton
import kotlin.time.Duration.Companion.seconds

/**
* SignalClient to LiveKit WS servers
Expand Down Expand Up @@ -184,10 +186,19 @@ constructor(
.addHeader("Authorization", "Bearer $token")
.build()

return suspendCancellableCoroutine {
// Wait for join response through WebSocketListener
joinContinuation = it
currentWs = websocketFactory.newWebSocket(request, this)
return withTimeout(5.seconds) {
suspendCancellableCoroutine { cont ->
// Wait for join response through WebSocketListener
joinContinuation = cont
// When a coroutine is canceled, WebSocket must be interrupted.
cont.invokeOnCancellation {
LKLog.w { "connect cancelled, abort websocket" }
currentWs?.cancel()
currentWs = null
joinContinuation = null
}
currentWs = websocketFactory.newWebSocket(request, this@SignalClient)
}
}
}

Expand Down Expand Up @@ -863,7 +874,7 @@ constructor(
pingJob = null
pongJob?.cancel()
pongJob = null
currentWs?.close(code, reason)
currentWs?.cancel()
currentWs = null
joinContinuation?.cancel()
joinContinuation = null
Expand Down