-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Dispatcher] Handle fallback to .org #1002
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,10 @@ final class Dispatcher: NSObject, Dispatching { | |
private let logger: ConsoleLogging | ||
|
||
private let defaultTimeout: Int = 5 | ||
|
||
/// The property is used to determine whether relay.walletconnect.org will be used | ||
/// in case relay.walletconnect.com doesn't respond for some reason (most likely due to being blocked in the user's location). | ||
private var fallback = false | ||
|
||
private let socketConnectionStatusPublisherSubject = CurrentValueSubject<SocketConnectionStatus, Never>(.disconnected) | ||
|
||
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> { | ||
|
@@ -38,7 +41,7 @@ final class Dispatcher: NSObject, Dispatching { | |
self.relayUrlFactory = relayUrlFactory | ||
self.logger = logger | ||
|
||
let socket = socketFactory.create(with: relayUrlFactory.create()) | ||
let socket = socketFactory.create(with: relayUrlFactory.create(fallback: fallback)) | ||
socket.request.addValue(EnvironmentInfo.userAgent, forHTTPHeaderField: "User-Agent") | ||
self.socket = socket | ||
|
||
|
@@ -72,10 +75,11 @@ final class Dispatcher: NSObject, Dispatching { | |
.filter { $0 == .connected } | ||
.setFailureType(to: NetworkError.self) | ||
.timeout(.seconds(defaultTimeout), scheduler: concurrentQueue, customError: { .webSocketNotConnected }) | ||
.sink(receiveCompletion: { result in | ||
.sink(receiveCompletion: { [unowned self] result in | ||
switch result { | ||
case .failure(let error): | ||
cancellable?.cancel() | ||
self.handleFallbackIfNeeded(error: error) | ||
completion(error) | ||
case .finished: break | ||
} | ||
|
@@ -104,7 +108,10 @@ final class Dispatcher: NSObject, Dispatching { | |
func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws { | ||
try socketConnectionHandler.handleDisconnect(closeCode: closeCode) | ||
} | ||
} | ||
|
||
// MARK: - Private functions | ||
extension Dispatcher { | ||
private func setUpWebSocketSession() { | ||
socket.onText = { [unowned self] in | ||
self.onMessage?($0) | ||
|
@@ -118,11 +125,22 @@ final class Dispatcher: NSObject, Dispatching { | |
socket.onDisconnect = { [unowned self] error in | ||
self.socketConnectionStatusPublisherSubject.send(.disconnected) | ||
if error != nil { | ||
self.socket.request.url = relayUrlFactory.create() | ||
self.socket.request.url = relayUrlFactory.create(fallback: fallback) | ||
} | ||
Task(priority: .high) { | ||
await self.socketConnectionHandler.handleDisconnection() | ||
} | ||
} | ||
} | ||
|
||
private func handleFallbackIfNeeded(error: NetworkError) { | ||
if error == .webSocketNotConnected && socket.request.url?.host == NetworkConstants.defaultUrl { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you help me understand the logic? it will fallback if socket did not connect and url is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check for Fallback will happen when the connection timeout happens, we just called this error |
||
logger.debug("[WebSocket] - Fallback to \(NetworkConstants.fallbackUrl)") | ||
fallback = true | ||
socket.request.url = relayUrlFactory.create(fallback: fallback) | ||
Task(priority: .high) { | ||
await self.socketConnectionHandler.handleDisconnection() | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum NetworkConstants { | ||
static var defaultUrl = "relay.walletconnect.com" | ||
static var fallbackUrl = "relay.walletconnect.org" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ struct RelayUrlFactory { | |
private let relayHost: String | ||
private let projectId: String | ||
private let socketAuthenticator: ClientIdAuthenticating | ||
|
||
init( | ||
relayHost: String, | ||
projectId: String, | ||
|
@@ -15,10 +15,10 @@ struct RelayUrlFactory { | |
self.socketAuthenticator = socketAuthenticator | ||
} | ||
|
||
func create() -> URL { | ||
func create(fallback: Bool) -> URL { | ||
var components = URLComponents() | ||
components.scheme = "wss" | ||
components.host = relayHost | ||
components.host = fallback ? NetworkConstants.fallbackUrl : relayHost | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this mean that if a wallet wants to enable fallback, for all it's users regardless of their location the url will be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the wallet doesn't have the option to enable/disable fallback. It happens automatically when the end user can't connect to |
||
components.queryItems = [ | ||
URLQueryItem(name: "projectId", value: projectId) | ||
] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we describe the purpose of the variable here, so it will be easier to get context later?