Skip to content

Commit

Permalink
Add option to configure timeout interval for socket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
patryk-sredzinski committed Dec 27, 2024
1 parent 9c5242f commit 6985cee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/SignalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ actor SignalClient: Loggable {
connectionState = (reconnectMode != nil ? .reconnecting : .connecting)

do {
let socket = try await WebSocket(url: url)
let socket = try await WebSocket(url: url, connectOptions: connectOptions)

_messageLoopTask = Task.detached {
self.log("Did enter WebSocket message loop...")
Expand Down
4 changes: 2 additions & 2 deletions Sources/LiveKit/Support/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate
waitForNextValue()
}

init(url: URL) async throws {
init(url: URL, connectOptions: ConnectOptions?) async throws {
request = URLRequest(url: url,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: .defaultSocketConnect)
timeoutInterval: connectOptions?.connectTimeoutInterval ?? .defaultSocketConnect)
super.init()
try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
Expand Down
9 changes: 9 additions & 0 deletions Sources/LiveKit/Types/Options/ConnectOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public final class ConnectOptions: NSObject, Sendable {
@objc
public let reconnectAttemptDelay: TimeInterval

/// The timeout interval for the room connection.
@objc
public let connectTimeoutInterval: TimeInterval

@objc
public let primaryTransportConnectTimeout: TimeInterval

Expand All @@ -54,6 +58,7 @@ public final class ConnectOptions: NSObject, Sendable {
autoSubscribe = true
reconnectAttempts = 3
reconnectAttemptDelay = .defaultReconnectAttemptDelay
connectTimeoutInterval = .defaultSocketConnect
primaryTransportConnectTimeout = .defaultTransportState
publisherTransportConnectTimeout = .defaultTransportState
iceServers = []
Expand All @@ -65,6 +70,7 @@ public final class ConnectOptions: NSObject, Sendable {
public init(autoSubscribe: Bool = true,
reconnectAttempts: Int = 3,
reconnectAttemptDelay: TimeInterval = .defaultReconnectAttemptDelay,
connectTimeoutInterval: TimeInterval = .defaultSocketConnect,
primaryTransportConnectTimeout: TimeInterval = .defaultTransportState,
publisherTransportConnectTimeout: TimeInterval = .defaultTransportState,
iceServers: [IceServer] = [],
Expand All @@ -74,6 +80,7 @@ public final class ConnectOptions: NSObject, Sendable {
self.autoSubscribe = autoSubscribe
self.reconnectAttempts = reconnectAttempts
self.reconnectAttemptDelay = reconnectAttemptDelay
self.connectTimeoutInterval = connectTimeoutInterval
self.primaryTransportConnectTimeout = primaryTransportConnectTimeout
self.publisherTransportConnectTimeout = publisherTransportConnectTimeout
self.iceServers = iceServers
Expand All @@ -88,6 +95,7 @@ public final class ConnectOptions: NSObject, Sendable {
return autoSubscribe == other.autoSubscribe &&
reconnectAttempts == other.reconnectAttempts &&
reconnectAttemptDelay == other.reconnectAttemptDelay &&
connectTimeoutInterval == other.connectTimeoutInterval &&
primaryTransportConnectTimeout == other.primaryTransportConnectTimeout &&
publisherTransportConnectTimeout == other.publisherTransportConnectTimeout &&
iceServers == other.iceServers &&
Expand All @@ -100,6 +108,7 @@ public final class ConnectOptions: NSObject, Sendable {
hasher.combine(autoSubscribe)
hasher.combine(reconnectAttempts)
hasher.combine(reconnectAttemptDelay)
hasher.combine(connectTimeoutInterval)
hasher.combine(primaryTransportConnectTimeout)
hasher.combine(publisherTransportConnectTimeout)
hasher.combine(iceServers)
Expand Down

0 comments on commit 6985cee

Please sign in to comment.