Skip to content

Commit

Permalink
selected url for every WebSocketEngine State
Browse files Browse the repository at this point in the history
  • Loading branch information
svojsu committed Jun 17, 2024
1 parent 874325d commit 011b5e9
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions SubstrateSdk/Classes/Network/WebSocketEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -170,7 +171,6 @@ public final class WebSocketEngine {
processingQueue: self.processingQueue,
connectionTimeout: connectionTimeout
)

connection.delegate = self

logger?.debug("(\(chainName)) Did set new urls: \(newUrls)")
Expand Down Expand Up @@ -206,7 +206,7 @@ public final class WebSocketEngine {

switch state {
case .connected:
state = .notConnected
state = .notConnected(url: selectedURL)

let cancelledRequests = resetInProgress()

Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 011b5e9

Please sign in to comment.