Refinements to RelayConnection plus tests for creating requests
Closes: #644
This commit is contained in:
committed by
William Casarin
parent
06a1a9aba6
commit
603a5a1814
@@ -278,21 +278,7 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
|
||||
return (self.flags & 1) != 0
|
||||
}
|
||||
|
||||
init(content: String, pubkey: String, kind: Int = 1, tags: [[String]] = [], createdAt: Int64 = Int64(Date().timeIntervalSince1970)) {
|
||||
self.id = ""
|
||||
self.sig = ""
|
||||
|
||||
self.content = content
|
||||
self.pubkey = pubkey
|
||||
self.kind = kind
|
||||
self.tags = tags
|
||||
self.created_at = createdAt
|
||||
}
|
||||
|
||||
/// Intiialization statement used to specificy ID
|
||||
///
|
||||
/// This is mainly used for contant and testing data
|
||||
init(id: String, content: String, pubkey: String, kind: Int = 1, tags: [[String]] = []) {
|
||||
init(id: String = "", content: String, pubkey: String, kind: Int = 1, tags: [[String]] = [], createdAt: Int64 = Int64(Date().timeIntervalSince1970)) {
|
||||
self.id = id
|
||||
self.sig = ""
|
||||
|
||||
@@ -300,7 +286,7 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
|
||||
self.pubkey = pubkey
|
||||
self.kind = kind
|
||||
self.tags = tags
|
||||
self.created_at = Int64(Date().timeIntervalSince1970)
|
||||
self.created_at = createdAt
|
||||
}
|
||||
|
||||
init(from: NostrEvent, content: String? = nil) {
|
||||
|
||||
@@ -13,42 +13,41 @@ enum NostrConnectionEvent {
|
||||
case nostr_event(NostrResponse)
|
||||
}
|
||||
|
||||
class RelayConnection: WebSocketDelegate {
|
||||
var isConnected: Bool = false
|
||||
var isConnecting: Bool = false
|
||||
var isReconnecting: Bool = false
|
||||
var last_connection_attempt: Double = 0
|
||||
var socket: WebSocket
|
||||
var handleEvent: (NostrConnectionEvent) -> ()
|
||||
let url: URL
|
||||
final class RelayConnection: WebSocketDelegate {
|
||||
private(set) var isConnected = false
|
||||
private(set) var isConnecting = false
|
||||
private(set) var isReconnecting = false
|
||||
|
||||
private(set) var last_connection_attempt: TimeInterval = 0
|
||||
private lazy var socket = {
|
||||
let req = URLRequest(url: url)
|
||||
let socket = WebSocket(request: req, compressionHandler: .none)
|
||||
socket.delegate = self
|
||||
return socket
|
||||
}()
|
||||
private var handleEvent: (NostrConnectionEvent) -> ()
|
||||
private let url: URL
|
||||
|
||||
init(url: URL, handleEvent: @escaping (NostrConnectionEvent) -> ()) {
|
||||
self.url = url
|
||||
self.handleEvent = handleEvent
|
||||
// just init, we don't actually use this one
|
||||
self.socket = make_websocket(url: url)
|
||||
}
|
||||
|
||||
func reconnect() {
|
||||
if self.isConnected {
|
||||
self.isReconnecting = true
|
||||
self.disconnect()
|
||||
if isConnected {
|
||||
isReconnecting = true
|
||||
disconnect()
|
||||
} else {
|
||||
// we're already disconnected, so just connect
|
||||
self.connect(force: true)
|
||||
connect(force: true)
|
||||
}
|
||||
}
|
||||
|
||||
func connect(force: Bool = false){
|
||||
if !force && (self.isConnected || self.isConnecting) {
|
||||
func connect(force: Bool = false) {
|
||||
if !force && (isConnected || isConnecting) {
|
||||
return
|
||||
}
|
||||
|
||||
var req = URLRequest(url: self.url)
|
||||
req.timeoutInterval = 5
|
||||
socket = make_websocket(url: url)
|
||||
socket.delegate = self
|
||||
|
||||
|
||||
isConnecting = true
|
||||
last_connection_attempt = Date().timeIntervalSince1970
|
||||
socket.connect()
|
||||
@@ -68,7 +67,9 @@ class RelayConnection: WebSocketDelegate {
|
||||
|
||||
socket.write(string: req)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - WebSocketDelegate
|
||||
|
||||
func didReceive(event: WebSocketEvent, client: WebSocket) {
|
||||
switch event {
|
||||
case .connected:
|
||||
@@ -83,8 +84,7 @@ class RelayConnection: WebSocketDelegate {
|
||||
self.connect()
|
||||
}
|
||||
|
||||
case .cancelled: fallthrough
|
||||
case .error:
|
||||
case .cancelled, .error:
|
||||
self.isConnecting = false
|
||||
self.isConnected = false
|
||||
|
||||
@@ -114,7 +114,6 @@ class RelayConnection: WebSocketDelegate {
|
||||
|
||||
handleEvent(.ws_event(event))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func make_nostr_req(_ req: NostrRequest) -> String? {
|
||||
@@ -138,7 +137,7 @@ func make_nostr_push_event(ev: NostrEvent) -> String? {
|
||||
}
|
||||
|
||||
func make_nostr_unsubscribe_req(_ sub_id: String) -> String? {
|
||||
return "[\"CLOSE\",\"\(sub_id)\"]"
|
||||
"[\"CLOSE\",\"\(sub_id)\"]"
|
||||
}
|
||||
|
||||
func make_nostr_subscription_req(_ filters: [NostrFilter], sub_id: String) -> String? {
|
||||
@@ -155,10 +154,3 @@ func make_nostr_subscription_req(_ filters: [NostrFilter], sub_id: String) -> St
|
||||
req += "]"
|
||||
return req
|
||||
}
|
||||
|
||||
func make_websocket(url: URL) -> WebSocket {
|
||||
let req = URLRequest(url: url)
|
||||
//req.setValue("chat,superchat", forHTTPHeaderField: "Sec-WebSocket-Protocol")
|
||||
return WebSocket(request: req, compressionHandler: .none)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user