damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | LICENSE

commit f314a4163fb6189a24dbae6a379c2f303159bff6
parent ec1db64370cb3bf629d09eb2ce0172eb7be1d598
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 17 Apr 2022 04:27:17 -0700

add explicit reconnect

otherwise we try to reconnect on a broken socket which doesn't seem to
work

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/ContentView.swift | 8++++----
Mdamus/Nostr/RelayConnection.swift | 42+++++++++++++++++++++++++++++++++++++-----
Mdamus/Nostr/RelayPool.swift | 7+++++++
3 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -220,15 +220,15 @@ struct ContentView: View { case .error(let merr): let desc = merr.debugDescription if desc.contains("Software caused connection abort") { - self.pool?.connect(to: [relay_id]) + self.pool?.reconnect(to: [relay_id]) } case .disconnected: - self.pool?.connect(to: [relay_id]) + self.pool?.reconnect(to: [relay_id]) case .cancelled: - self.pool?.connect(to: [relay_id]) + self.pool?.reconnect(to: [relay_id]) case .reconnectSuggested(let t): if t { - self.pool?.connect(to: [relay_id]) + self.pool?.reconnect(to: [relay_id]) } default: break diff --git a/damus/Nostr/RelayConnection.swift b/damus/Nostr/RelayConnection.swift @@ -15,24 +15,47 @@ enum NostrConnectionEvent { class RelayConnection: WebSocketDelegate { var isConnected: Bool = false + var isConnecting: Bool = false + var isReconnecting: Bool = false var socket: WebSocket var handleEvent: (NostrConnectionEvent) -> () + let url: URL init(url: URL, handleEvent: @escaping (NostrConnectionEvent) -> ()) { - var req = URLRequest(url: url) - req.timeoutInterval = 5 - self.socket = WebSocket(request: req, certPinner: nil, compressionHandler: .none, useCustomEngine: true) + self.url = url self.handleEvent = handleEvent + // just init, we don't actually use this one + self.socket = WebSocket(request: URLRequest(url: self.url), compressionHandler: .none) + } - socket.delegate = self + func reconnect() { + if self.isConnected { + self.isReconnecting = true + self.disconnect() + } else { + // we're already disconnected, so just connect + self.connect() + } } func connect(){ + if self.isConnected || self.isConnecting { + return + } + + var req = URLRequest(url: self.url) + req.timeoutInterval = 5 + socket = WebSocket(request: req, compressionHandler: .none) + socket.delegate = self + + isConnecting = true socket.connect() } func disconnect() { socket.disconnect() + isConnected = false + isConnecting = false } func send(_ req: NostrRequest) { @@ -49,10 +72,19 @@ class RelayConnection: WebSocketDelegate { switch event { case .connected: self.isConnected = true + self.isConnecting = false + + case .disconnected: + self.isConnecting = false + self.isConnected = false + if self.isReconnecting { + self.isReconnecting = false + self.connect() + } - case .disconnected: fallthrough case .cancelled: fallthrough case .error: + self.isConnecting = false self.isConnected = false case .text(let txt): diff --git a/damus/Nostr/RelayPool.swift b/damus/Nostr/RelayPool.swift @@ -57,6 +57,13 @@ class RelayPool { self.relays.append(relay) } + func reconnect(to: [String]? = nil) { + let relays = to.map{ get_relays($0) } ?? self.relays + for relay in relays { + relay.connection.reconnect() + } + } + func connect(to: [String]? = nil) { let relays = to.map{ get_relays($0) } ?? self.relays for relay in relays {