This commit is contained in:
Marcos Rodriguez Velez 2023-03-23 20:13:15 -04:00
parent c284a716e4
commit e55bc4eaa7
2 changed files with 8 additions and 7 deletions

View file

@ -14,7 +14,7 @@ class TCPClient: NSObject {
private let bufferSize = 1024
// Define the completion block type
typealias ReceiveCompletion = (Result<String, Error>) -> Void
typealias ReceiveCompletion = (Result<Data, Error>) -> Void
// Add a completion block property
var receiveCompletion: ReceiveCompletion?
@ -87,8 +87,8 @@ extension TCPClient: StreamDelegate {
while inputStream.hasBytesAvailable {
let bytesRead = inputStream.read(buffer, maxLength: bufferSize)
if bytesRead > 0 {
et data = Data(bytes: buffer, count: bytesRead)
receiveCompletion?(.success(receivedString))
let data = Data(bytes: buffer, count: bytesRead)
receiveCompletion?(.success(data))
}
}
}

View file

@ -6,6 +6,8 @@
// Copyright © 2020 BlueWallet. All rights reserved.
//
import Foundation
struct APIError: LocalizedError {
var errorDescription: String = "Failed to fetch Electrum data..."
}
@ -22,8 +24,8 @@ extension WidgetAPI {
client.receiveCompletion = { result in
switch result {
case .success(let data):
print("Received: \(string)")
guard let let response = String(bytes: data, encoding: .utf8)?.data(using: .utf8) else {
print("Received: \(data)")
guard let response = String(bytes: data, encoding: .utf8)?.data(using: .utf8) else {
client.close()
completion(nil, APIError())
return
@ -47,7 +49,7 @@ extension WidgetAPI {
}
}
if client.connect(to: host, port: port) {
if client.connect(to: host, port: UInt32(port)) {
let message = "{\"id\": 1, \"method\": \"blockchain.estimatefee\", \"params\": [1]}\n"
if let data = message.data(using: .utf8), client.send(data: data) {
print("Message sent!")
@ -56,7 +58,6 @@ extension WidgetAPI {
client.close()
} else {
print("Connection failed")
print(error)
client.close()
if userElectrumSettings.host == DefaultElectrumPeers.last?.host {
completion(nil, APIError())