i broke it :)
This commit is contained in:
@@ -9,30 +9,21 @@ protocol SocketDelegate: AnyObject {
|
||||
public class Socket: NSObject {
|
||||
|
||||
var id: String
|
||||
var host: String
|
||||
var port: Int
|
||||
var useTLS: Bool
|
||||
var acceptInvalidCertificates: Bool
|
||||
|
||||
var connection: NWConnection?
|
||||
|
||||
weak var delegate: SocketDelegate?
|
||||
|
||||
public init(id: String, host: String, port: Int, useTLS: Bool, acceptInvalidCertificates: Bool) {
|
||||
public init(id: String) {
|
||||
self.id = id
|
||||
self.host = host
|
||||
self.port = port
|
||||
|
||||
self.useTLS = useTLS
|
||||
self.acceptInvalidCertificates = acceptInvalidCertificates
|
||||
}
|
||||
|
||||
public func connect() {
|
||||
public func connect(host: String, port: Int, useTLS: Bool, acceptInvalidCertificates: Bool) {
|
||||
let parameters = NWParameters.tcp
|
||||
if self.useTLS {
|
||||
if useTLS {
|
||||
let tls = NWProtocolTLS.Options()
|
||||
|
||||
if (self.acceptInvalidCertificates) {
|
||||
if (acceptInvalidCertificates) {
|
||||
sec_protocol_options_set_verify_block(tls.securityProtocolOptions, { (sec_protocol_metadata, sec_trust, sec_protocol_verify_complete) in
|
||||
sec_protocol_verify_complete(true)
|
||||
}, DispatchQueue.global())
|
||||
@@ -41,7 +32,7 @@ public class Socket: NSObject {
|
||||
parameters.defaultProtocolStack.applicationProtocols.insert(tls, at: 0)
|
||||
}
|
||||
|
||||
let connection = NWConnection(host: NWEndpoint.Host(self.host), port: NWEndpoint.Port(String(self.port))!, using: parameters)
|
||||
let connection = NWConnection(host: NWEndpoint.Host(host), port: NWEndpoint.Port(String(port))!, using: parameters)
|
||||
connection.stateUpdateHandler = self.stateDidChange(to:)
|
||||
self.receive(on: connection)
|
||||
connection.start(queue: .main)
|
||||
|
||||
@@ -29,16 +29,16 @@ enum SocketState:String {
|
||||
])
|
||||
}
|
||||
|
||||
@objc public func create(id: String, host: String, port: Int, useTLS: Bool = false, acceptInvalidCertificates: Bool = false) -> Socket {
|
||||
let socket = Socket(id: id, host: host, port: port, useTLS: useTLS, acceptInvalidCertificates: acceptInvalidCertificates)
|
||||
@objc public func create(id: String) -> Socket {
|
||||
let socket = Socket(id: id)
|
||||
socket.delegate = self
|
||||
sockets.append(socket)
|
||||
return socket
|
||||
}
|
||||
|
||||
@objc public func connect(id: String) {
|
||||
@objc public func connect(id: String, host: String, port: Int, useTLS: Bool = false, acceptInvalidCertificates: Bool = false) {
|
||||
if let socket = self.socket(with: id) {
|
||||
socket.connect()
|
||||
socket.connect(host: host, port: port, useTLS: useTLS, acceptInvalidCertificates: acceptInvalidCertificates)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,19 +19,17 @@ public class SocketsPlugin: CAPPlugin, CAPBridgedPlugin {
|
||||
|
||||
@objc func create(_ call: CAPPluginCall) {
|
||||
let id = call.getString("id") ?? UUID().uuidString
|
||||
let host = call.getString("host") ?? ""
|
||||
let port = call.getInt("port") ?? 0
|
||||
|
||||
let useTLS = call.getBool("useTLS") ?? false
|
||||
let acceptInvalidCertificates = call.getBool("acceptInvalidCertificates") ?? false
|
||||
|
||||
let socket = implementation.create(id: id, host: host, port: port, useTLS: useTLS, acceptInvalidCertificates: acceptInvalidCertificates)
|
||||
let socket = implementation.create(id: id)
|
||||
call.resolve()
|
||||
}
|
||||
|
||||
@objc func connect(_ call: CAPPluginCall) {
|
||||
let id = call.getString("id") ?? ""
|
||||
implementation.connect(id: id)
|
||||
let host = call.getString("host") ?? ""
|
||||
let port = call.getInt("port") ?? 0
|
||||
let useTLS = call.getBool("useTLS") ?? false
|
||||
let acceptInvalidCertificates = call.getBool("acceptInvalidCertificates") ?? false
|
||||
implementation.connect(id: id, host: host, port: port, useTLS: useTLS, acceptInvalidCertificates: acceptInvalidCertificates)
|
||||
call.resolve()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user