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:
committed by
William Casarin
parent
ef4aeb40e0
commit
40e5e4a026
@@ -49,6 +49,7 @@ final class RelayConnection: ObservableObject {
|
|||||||
|
|
||||||
private var handleEvent: (NostrConnectionEvent) -> ()
|
private var handleEvent: (NostrConnectionEvent) -> ()
|
||||||
private let url: RelayURL
|
private let url: RelayURL
|
||||||
|
var log: RelayLog?
|
||||||
|
|
||||||
init(url: RelayURL, handleEvent: @escaping (NostrConnectionEvent) -> ()) {
|
init(url: RelayURL, handleEvent: @escaping (NostrConnectionEvent) -> ()) {
|
||||||
self.url = url
|
self.url = url
|
||||||
@@ -59,11 +60,13 @@ final class RelayConnection: ObservableObject {
|
|||||||
socket.ping { err in
|
socket.ping { err in
|
||||||
if err == nil {
|
if err == nil {
|
||||||
self.last_pong = .now
|
self.last_pong = .now
|
||||||
|
self.log?.add("Successful ping")
|
||||||
} else {
|
} else {
|
||||||
print("pong failed, reconnecting \(self.url.id)")
|
print("pong failed, reconnecting \(self.url.id)")
|
||||||
self.isConnected = false
|
self.isConnected = false
|
||||||
self.isConnecting = false
|
self.isConnecting = false
|
||||||
self.reconnect_with_backoff()
|
self.reconnect_with_backoff()
|
||||||
|
self.log?.add("Ping failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,6 +156,10 @@ final class RelayConnection: ObservableObject {
|
|||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.handleEvent(.ws_event(event))
|
self.handleEvent(.ws_event(event))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let description = event.description {
|
||||||
|
log?.add(description)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func reconnect_with_backoff() {
|
func reconnect_with_backoff() {
|
||||||
@@ -166,6 +173,7 @@ final class RelayConnection: ObservableObject {
|
|||||||
}
|
}
|
||||||
disconnect()
|
disconnect()
|
||||||
connect()
|
connect()
|
||||||
|
log?.add("Reconnecting...")
|
||||||
}
|
}
|
||||||
|
|
||||||
func reconnect_in(after: TimeInterval) {
|
func reconnect_in(after: TimeInterval) {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ enum WebSocketEvent {
|
|||||||
case .connected:
|
case .connected:
|
||||||
return "Connected"
|
return "Connected"
|
||||||
case .message(_):
|
case .message(_):
|
||||||
return "Received message"
|
return nil // adding this to the RelayLog was too noisy
|
||||||
case .disconnected(let close_code, let reason):
|
case .disconnected(let close_code, let reason):
|
||||||
return "Disconnected: Close code: \(close_code), reason: \(reason ?? "unknown")"
|
return "Disconnected: Close code: \(close_code), reason: \(reason ?? "unknown")"
|
||||||
case .error(let error):
|
case .error(let error):
|
||||||
|
|||||||
Reference in New Issue
Block a user