Browse Source

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>
profiles-everywhere
William Casarin 3 years ago
parent
commit
f314a4163f
  1. 8
      damus/ContentView.swift
  2. 42
      damus/Nostr/RelayConnection.swift
  3. 7
      damus/Nostr/RelayPool.swift

8
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

42
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):

7
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 {

Loading…
Cancel
Save