From 6985ceeb33dd2faeffdbe4ac0598932ecdae1dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20S=CC=81redzin=CC=81ski?= Date: Fri, 27 Dec 2024 21:37:13 +0100 Subject: [PATCH] Add option to configure timeout interval for socket connection --- Sources/LiveKit/Core/SignalClient.swift | 2 +- Sources/LiveKit/Support/WebSocket.swift | 4 ++-- Sources/LiveKit/Types/Options/ConnectOptions.swift | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Sources/LiveKit/Core/SignalClient.swift b/Sources/LiveKit/Core/SignalClient.swift index 52ef3ad5e..c5409f938 100644 --- a/Sources/LiveKit/Core/SignalClient.swift +++ b/Sources/LiveKit/Core/SignalClient.swift @@ -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...") diff --git a/Sources/LiveKit/Support/WebSocket.swift b/Sources/LiveKit/Support/WebSocket.swift index 9cd2da1ed..88d00ec42 100644 --- a/Sources/LiveKit/Support/WebSocket.swift +++ b/Sources/LiveKit/Support/WebSocket.swift @@ -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 diff --git a/Sources/LiveKit/Types/Options/ConnectOptions.swift b/Sources/LiveKit/Types/Options/ConnectOptions.swift index dd303b311..c9845cd87 100644 --- a/Sources/LiveKit/Types/Options/ConnectOptions.swift +++ b/Sources/LiveKit/Types/Options/ConnectOptions.swift @@ -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 @@ -54,6 +58,7 @@ public final class ConnectOptions: NSObject, Sendable { autoSubscribe = true reconnectAttempts = 3 reconnectAttemptDelay = .defaultReconnectAttemptDelay + connectTimeoutInterval = .defaultSocketConnect primaryTransportConnectTimeout = .defaultTransportState publisherTransportConnectTimeout = .defaultTransportState iceServers = [] @@ -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] = [], @@ -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 @@ -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 && @@ -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)