commit bcaa1d2354649b89c47277cecde55cc57cc7ca73
parent 296d96d6dfa7172758d284be48aecc2c0ce0b120
Author: William Casarin <jb55@jb55.com>
Date: Tue, 20 Jun 2023 11:21:07 +0200
ping: switch to async style
because reasons
Diffstat:
2 files changed, 18 insertions(+), 7 deletions(-)
diff --git a/damus/Nostr/RelayConnection.swift b/damus/Nostr/RelayConnection.swift
@@ -56,15 +56,18 @@ final class RelayConnection: ObservableObject {
}
func ping() {
- socket.ping { err in
- if err == nil {
- self.last_pong = .now
- } else {
- print("pong failed, reconnecting \(self.url.id)")
+ Task {
+ do {
+ try await socket.ping()
+ } catch {
+ print("pong failed (\(error)), reconnecting \(self.url.id)")
self.isConnected = false
self.isConnecting = false
self.reconnect_with_backoff()
+ return
}
+
+ self.last_pong = .now
}
}
diff --git a/damus/Nostr/WebSocket.swift b/damus/Nostr/WebSocket.swift
@@ -45,8 +45,16 @@ final class WebSocket: NSObject, URLSessionWebSocketDelegate {
self.session = session
}
- func ping(receiveHandler: @escaping (Error?) -> Void) {
- self.webSocketTask.sendPing(pongReceiveHandler: receiveHandler)
+ func ping() async throws -> () {
+ return try await withCheckedThrowingContinuation { cont in
+ self.webSocketTask.sendPing { err in
+ if let err {
+ cont.resume(throwing: err)
+ } else {
+ cont.resume(returning: ())
+ }
+ }
+ }
}
func connect() {