From 011b5e95fb8ce717bd4e8358e9a46948eb3687c8 Mon Sep 17 00:00:00 2001 From: svojsu Date: Mon, 17 Jun 2024 20:58:52 +0300 Subject: [PATCH] selected url for every WebSocketEngine State --- .../Classes/Network/WebSocketEngine.swift | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/SubstrateSdk/Classes/Network/WebSocketEngine.swift b/SubstrateSdk/Classes/Network/WebSocketEngine.swift index db68211..e457278 100644 --- a/SubstrateSdk/Classes/Network/WebSocketEngine.swift +++ b/SubstrateSdk/Classes/Network/WebSocketEngine.swift @@ -25,9 +25,9 @@ public protocol WebSocketEngineDelegate: AnyObject { public final class WebSocketEngine { public enum State: Equatable { - case notConnected - case connecting - case waitingReconnection + case notConnected(url: URL?) + case connecting(url: URL) + case waitingReconnection(url: URL) case connected(url: URL) } @@ -43,7 +43,7 @@ public final class WebSocketEngine { public let pingInterval: TimeInterval public let connectionTimeout: TimeInterval - public private(set) var state: State = .notConnected { + public private(set) var state: State { didSet { if let delegate = delegate { let oldState = oldValue @@ -124,6 +124,7 @@ public final class WebSocketEngine { self.pingInterval = pingInterval self.connectionTimeout = connectionTimeout self.selectedURLIndex = 0 + self.state = .notConnected(url: urls[selectedURLIndex]) guard let url = urls.first else { return nil @@ -170,7 +171,6 @@ public final class WebSocketEngine { processingQueue: self.processingQueue, connectionTimeout: connectionTimeout ) - connection.delegate = self logger?.debug("(\(chainName)) Did set new urls: \(newUrls)") @@ -206,7 +206,7 @@ public final class WebSocketEngine { switch state { case .connected: - state = .notConnected + state = .notConnected(url: selectedURL) let cancelledRequests = resetInProgress() @@ -225,14 +225,14 @@ public final class WebSocketEngine { logger?.debug("(\(chainName):\(selectedURL)) Did start disconnect from socket") case .connecting: - state = .notConnected + state = .notConnected(url: selectedURL) forceConnectionReset() logger?.debug("(\(chainName):\(selectedURL)) Cancel socket connection") case .waitingReconnection: - state = .notConnected + state = .notConnected(url: selectedURL) forceConnectionReset() reconnectionScheduler.cancel() @@ -668,7 +668,7 @@ extension WebSocketEngine { if let reconnectionStrategy = reconnectionStrategy, let nextDelay = reconnectionStrategy.reconnectAfter(attempt: actualAttempt) { - state = .waitingReconnection + state = .waitingReconnection(url: selectedURL) let chainName = "\(chainName):\(selectedURL)" logger?.debug( @@ -677,7 +677,7 @@ extension WebSocketEngine { reconnectionScheduler.notifyAfter(nextDelay) } else { - state = .notConnected + state = .notConnected(url: selectedURL) // notify pendings about error because there is no chance to reconnect @@ -791,7 +791,7 @@ extension WebSocketEngine { logger?.debug("(\(chainName):\(selectedURL)) Start connecting with attempt: \(attempt)") updateReconnectionAttempts(attempt, for: selectedURL) - state = .connecting + state = .connecting(url: selectedURL) connection.connect() }