add a RelayLog to each RelayConnection and send events to it

Signed-off-by: Bryan Montz <bryanmontz@me.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Bryan Montz
2023-07-09 08:45:34 -05:00
committed by William Casarin
parent ef4aeb40e0
commit 40e5e4a026
2 changed files with 9 additions and 1 deletions

View File

@@ -49,6 +49,7 @@ final class RelayConnection: ObservableObject {
private var handleEvent: (NostrConnectionEvent) -> ()
private let url: RelayURL
var log: RelayLog?
init(url: RelayURL, handleEvent: @escaping (NostrConnectionEvent) -> ()) {
self.url = url
@@ -59,11 +60,13 @@ final class RelayConnection: ObservableObject {
socket.ping { err in
if err == nil {
self.last_pong = .now
self.log?.add("Successful ping")
} else {
print("pong failed, reconnecting \(self.url.id)")
self.isConnected = false
self.isConnecting = false
self.reconnect_with_backoff()
self.log?.add("Ping failed")
}
}
}
@@ -153,6 +156,10 @@ final class RelayConnection: ObservableObject {
DispatchQueue.main.async {
self.handleEvent(.ws_event(event))
}
if let description = event.description {
log?.add(description)
}
}
func reconnect_with_backoff() {
@@ -166,6 +173,7 @@ final class RelayConnection: ObservableObject {
}
disconnect()
connect()
log?.add("Reconnecting...")
}
func reconnect_in(after: TimeInterval) {

View File

@@ -19,7 +19,7 @@ enum WebSocketEvent {
case .connected:
return "Connected"
case .message(_):
return "Received message"
return nil // adding this to the RelayLog was too noisy
case .disconnected(let close_code, let reason):
return "Disconnected: Close code: \(close_code), reason: \(reason ?? "unknown")"
case .error(let error):