diff --git a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift index eef6758de..e3888e786 100644 --- a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift +++ b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift @@ -52,9 +52,7 @@ final class RelayClientEndToEndTests: XCTestCase { socketAuthenticator: socketAuthenticator ) - let socketUrlFallbackHandler = SocketUrlFallbackHandler(relayUrlFactory: relayUrlFactory, logger: logger, socket: socket, networkMonitor: networkMonitor) - - let socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket, logger: logger, socketUrlFallbackHandler: socketUrlFallbackHandler) + let socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket, logger: logger) let dispatcher = Dispatcher( socketFactory: webSocketFactory, relayUrlFactory: urlFactory, diff --git a/Sources/WalletConnectRelay/Misc/NetworkConstants.swift b/Sources/WalletConnectRelay/Misc/NetworkConstants.swift index 5a23a21c8..9f2d6f259 100644 --- a/Sources/WalletConnectRelay/Misc/NetworkConstants.swift +++ b/Sources/WalletConnectRelay/Misc/NetworkConstants.swift @@ -1,4 +1,3 @@ enum NetworkConstants { - static var defaultUrl = "relay.walletconnect.com" - static var fallbackUrl = "relay.walletconnect.org" + static var defaultUrl = "relay.walletconnect.org" } diff --git a/Sources/WalletConnectRelay/RelayClientFactory.swift b/Sources/WalletConnectRelay/RelayClientFactory.swift index 3a4097336..2120b720e 100644 --- a/Sources/WalletConnectRelay/RelayClientFactory.swift +++ b/Sources/WalletConnectRelay/RelayClientFactory.swift @@ -61,16 +61,11 @@ public struct RelayClientFactory { if let bundleId = Bundle.main.bundleIdentifier { socket.request.addValue(bundleId, forHTTPHeaderField: "Origin") } - let socketFallbackHandler = SocketUrlFallbackHandler( - relayUrlFactory: relayUrlFactory, - logger: logger, - socket: socket, - networkMonitor: networkMonitor - ) + var socketConnectionHandler: SocketConnectionHandler! switch socketConnectionType { - case .automatic: socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket, logger: logger, socketUrlFallbackHandler: socketFallbackHandler) - case .manual: socketConnectionHandler = ManualSocketConnectionHandler(socket: socket, logger: logger, socketUrlFallbackHandler: socketFallbackHandler) + case .automatic: socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket, logger: logger) + case .manual: socketConnectionHandler = ManualSocketConnectionHandler(socket: socket, logger: logger) } let dispatcher = Dispatcher( diff --git a/Sources/WalletConnectRelay/RelayURLFactory.swift b/Sources/WalletConnectRelay/RelayURLFactory.swift index 20290fd24..5809c89fa 100644 --- a/Sources/WalletConnectRelay/RelayURLFactory.swift +++ b/Sources/WalletConnectRelay/RelayURLFactory.swift @@ -18,19 +18,15 @@ class RelayUrlFactory { self.socketAuthenticator = socketAuthenticator } - func setFallback() { - self.fallback = true - } - func create() -> URL { var components = URLComponents() components.scheme = "wss" - components.host = fallback ? NetworkConstants.fallbackUrl : relayHost + components.host = relayHost components.queryItems = [ URLQueryItem(name: "projectId", value: projectId) ] do { - let authToken = try socketAuthenticator.createAuthToken(url: fallback ? "wss://" + NetworkConstants.fallbackUrl : "wss://" + relayHost) + let authToken = try socketAuthenticator.createAuthToken(url: "wss://" + relayHost) components.queryItems?.append(URLQueryItem(name: "auth", value: authToken)) } catch { // TODO: Handle token creation errors diff --git a/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift b/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift index 3be94af13..c9ea12219 100644 --- a/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift +++ b/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift @@ -14,9 +14,8 @@ class AutomaticSocketConnectionHandler { private let appStateObserver: AppStateObserving private let networkMonitor: NetworkMonitoring private let backgroundTaskRegistrar: BackgroundTaskRegistering - private let defaultTimeout: Int = 5 + private let defaultTimeout: Int = 60 private let logger: ConsoleLogging - private var socketUrlFallbackHandler: SocketUrlFallbackHandler private var publishers = Set() private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.automatic_socket_connection", qos: .utility, attributes: .concurrent) @@ -26,25 +25,17 @@ class AutomaticSocketConnectionHandler { networkMonitor: NetworkMonitoring = NetworkMonitor(), appStateObserver: AppStateObserving = AppStateObserver(), backgroundTaskRegistrar: BackgroundTaskRegistering = BackgroundTaskRegistrar(), - logger: ConsoleLogging, - socketUrlFallbackHandler: SocketUrlFallbackHandler + logger: ConsoleLogging ) { self.appStateObserver = appStateObserver self.socket = socket self.networkMonitor = networkMonitor self.backgroundTaskRegistrar = backgroundTaskRegistrar self.logger = logger - self.socketUrlFallbackHandler = socketUrlFallbackHandler setUpStateObserving() setUpNetworkMonitoring() - socketUrlFallbackHandler.onTryReconnect = { [unowned self] in - Task(priority: .high) { - await tryReconect() - } - } - connect() } @@ -62,8 +53,8 @@ class AutomaticSocketConnectionHandler { return } if !self.socket.isConnected { - self.logger.debug("Connection timed out, initiating fallback...") - self.socketUrlFallbackHandler.handleFallbackIfNeeded(error: .connectionFailed) + self.logger.debug("Connection timed out, will rety to connect...") + retryToConnect() } timer.cancel() } @@ -99,6 +90,12 @@ class AutomaticSocketConnectionHandler { socket.disconnect() } + private func retryToConnect() { + if !socket.isConnected { + connect() + } + } + private func reconnectIfNeeded() { if !socket.isConnected { socket.connect() @@ -109,11 +106,6 @@ class AutomaticSocketConnectionHandler { // MARK: - SocketConnectionHandler extension AutomaticSocketConnectionHandler: SocketConnectionHandler { - func tryReconect() async { - guard await appStateObserver.currentState == .foreground else { return } - reconnectIfNeeded() - } - func handleConnect() throws { throw Errors.manualSocketConnectionForbidden } diff --git a/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift b/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift index d0589ca9e..04152bd21 100644 --- a/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift +++ b/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift @@ -4,23 +4,14 @@ class ManualSocketConnectionHandler: SocketConnectionHandler { private let socket: WebSocketConnecting private let logger: ConsoleLogging - private let defaultTimeout: Int = 5 - private var socketUrlFallbackHandler: SocketUrlFallbackHandler + private let defaultTimeout: Int = 60 private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.manual_socket_connection", attributes: .concurrent) init( socket: WebSocketConnecting, - logger: ConsoleLogging, - socketUrlFallbackHandler: SocketUrlFallbackHandler) { + logger: ConsoleLogging) { self.socket = socket self.logger = logger - self.socketUrlFallbackHandler = socketUrlFallbackHandler - - socketUrlFallbackHandler.onTryReconnect = { [unowned self] in - Task(priority: .high) { - await tryReconect() - } - } } func handleConnect() throws { @@ -34,8 +25,8 @@ class ManualSocketConnectionHandler: SocketConnectionHandler { return } if !self.socket.isConnected { - self.logger.debug("Connection timed out, initiating fallback...") - self.socketUrlFallbackHandler.handleFallbackIfNeeded(error: .connectionFailed) + self.logger.debug("Connection timed out, will rety to connect...") + retryToConnect() } timer.cancel() } @@ -51,7 +42,7 @@ class ManualSocketConnectionHandler: SocketConnectionHandler { // ManualSocketConnectionHandler does not support reconnection logic } - func tryReconect() async { + private func retryToConnect() { if !socket.isConnected { socket.connect() } diff --git a/Sources/WalletConnectRelay/SocketConnectionHandler/SocketConnectionHandler.swift b/Sources/WalletConnectRelay/SocketConnectionHandler/SocketConnectionHandler.swift index 91284893b..4ac3046dd 100644 --- a/Sources/WalletConnectRelay/SocketConnectionHandler/SocketConnectionHandler.swift +++ b/Sources/WalletConnectRelay/SocketConnectionHandler/SocketConnectionHandler.swift @@ -4,5 +4,4 @@ protocol SocketConnectionHandler { func handleConnect() throws func handleDisconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws func handleDisconnection() async - func tryReconect() async } diff --git a/Sources/WalletConnectRelay/SocketUrlFallbackHandler.swift b/Sources/WalletConnectRelay/SocketUrlFallbackHandler.swift deleted file mode 100644 index dea30eecd..000000000 --- a/Sources/WalletConnectRelay/SocketUrlFallbackHandler.swift +++ /dev/null @@ -1,29 +0,0 @@ -import Foundation - -class SocketUrlFallbackHandler { - private let relayUrlFactory: RelayUrlFactory - private var logger: ConsoleLogging - private var socket: WebSocketConnecting - private let networkMonitor: NetworkMonitoring - var onTryReconnect: (()->())? - - init( - relayUrlFactory: RelayUrlFactory, - logger: ConsoleLogging, - socket: WebSocketConnecting, - networkMonitor: NetworkMonitoring) { - self.relayUrlFactory = relayUrlFactory - self.logger = logger - self.socket = socket - self.networkMonitor = networkMonitor - } - - func handleFallbackIfNeeded(error: NetworkError) { - if error == .connectionFailed && socket.request.url?.host == NetworkConstants.defaultUrl { - logger.debug("[WebSocket] - Fallback to \(NetworkConstants.fallbackUrl)") - relayUrlFactory.setFallback() - socket.request.url = relayUrlFactory.create() - onTryReconnect?() - } - } -} diff --git a/Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift b/Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift index b29a830ba..368d25da4 100644 --- a/Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift +++ b/Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift @@ -28,14 +28,12 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase { socketAuthenticator: socketAuthenticator ) backgroundTaskRegistrar = BackgroundTaskRegistrarMock() - let socketUrlFallbackHandler = SocketUrlFallbackHandler(relayUrlFactory: relayUrlFactory, logger: ConsoleLoggerMock(), socket: webSocket, networkMonitor: networkMonitor) sut = AutomaticSocketConnectionHandler( socket: webSocketSession, networkMonitor: networkMonitor, appStateObserver: appStateObserver, backgroundTaskRegistrar: backgroundTaskRegistrar, - logger: ConsoleLoggerMock(), - socketUrlFallbackHandler: socketUrlFallbackHandler + logger: ConsoleLoggerMock() ) } diff --git a/Tests/RelayerTests/DispatcherTests.swift b/Tests/RelayerTests/DispatcherTests.swift index 4a58cfd97..e8b0de168 100644 --- a/Tests/RelayerTests/DispatcherTests.swift +++ b/Tests/RelayerTests/DispatcherTests.swift @@ -71,8 +71,7 @@ final class DispatcherTests: XCTestCase { projectId: "1012db890cf3cfb0c1cdc929add657ba", socketAuthenticator: socketAuthenticator ) - let socketUrlFallbackHandler = SocketUrlFallbackHandler(relayUrlFactory: relayUrlFactory, logger: logger, socket: webSocket, networkMonitor: networkMonitor) - let socketConnectionHandler = ManualSocketConnectionHandler(socket: webSocket, logger: logger, socketUrlFallbackHandler: socketUrlFallbackHandler) + let socketConnectionHandler = ManualSocketConnectionHandler(socket: webSocket, logger: logger) sut = Dispatcher( socketFactory: webSocketFactory, relayUrlFactory: relayUrlFactory, diff --git a/Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift b/Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift index 6f8a939cb..d86fbc9cf 100644 --- a/Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift +++ b/Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift @@ -22,9 +22,8 @@ final class ManualSocketConnectionHandlerTests: XCTestCase { projectId: "1012db890cf3cfb0c1cdc929add657ba", socketAuthenticator: socketAuthenticator ) - let socketUrlFallbackHandler = SocketUrlFallbackHandler(relayUrlFactory: relayUrlFactory, logger: ConsoleLoggerMock(), socket: socket, networkMonitor: networkMonitor) - sut = ManualSocketConnectionHandler(socket: socket, logger: ConsoleLoggerMock(), socketUrlFallbackHandler: socketUrlFallbackHandler) + sut = ManualSocketConnectionHandler(socket: socket, logger: ConsoleLoggerMock()) } func testHandleDisconnect() {