diff --git a/README.md b/README.md index 2120535..2d9e0ce 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ npx cap sync * [`connect(...)`](#connect) * [`send(...)`](#send) * [`disconnect(...)`](#disconnect) +* [`close(...)`](#close) * [`addListener('state', ...)`](#addlistenerstate-) * [`addListener('message', ...)`](#addlistenermessage-) * [Interfaces](#interfaces) @@ -78,10 +79,23 @@ disconnect(options: { id: string; }) => Promise -------------------- +### close(...) + +```typescript +close(options: { id: string; }) => Promise +``` + +| Param | Type | +| ------------- | ---------------------------- | +| **`options`** | { id: string; } | + +-------------------- + + ### addListener('state', ...) ```typescript -addListener(eventName: 'state', listenerFunc: (message: { id: string; state: string; }) => void) => Promise +addListener(eventName: "state", listenerFunc: (message: { id: string; state: string; }) => void) => Promise ``` | Param | Type | @@ -97,7 +111,7 @@ addListener(eventName: 'state', listenerFunc: (message: { id: string; state: str ### addListener('message', ...) ```typescript -addListener(eventName: 'message', listenerFunc: (message: { id: string; message: string; }) => void) => Promise +addListener(eventName: "message", listenerFunc: (message: { id: string; message: string; }) => void) => Promise ``` | Param | Type | diff --git a/ios/Sources/SocketsPlugin/Socket.swift b/ios/Sources/SocketsPlugin/Socket.swift index 1f36ea1..a8335d6 100644 --- a/ios/Sources/SocketsPlugin/Socket.swift +++ b/ios/Sources/SocketsPlugin/Socket.swift @@ -28,7 +28,20 @@ public class Socket: NSObject { } public func connect() { - let connection = NWConnection(host: NWEndpoint.Host(self.host), port: NWEndpoint.Port(String(self.port))!, using: .tcp) + let parameters = NWParameters.tcp + if self.useTLS { + let tls = NWProtocolTLS.Options() + + if (self.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()) + } + + parameters.defaultProtocolStack.applicationProtocols.insert(tls, at: 0) + } + + let connection = NWConnection(host: NWEndpoint.Host(self.host), port: NWEndpoint.Port(String(self.port))!, using: parameters) connection.stateUpdateHandler = self.stateDidChange(to:) self.receive(on: connection) connection.start(queue: .main) @@ -84,7 +97,10 @@ public class Socket: NSObject { self.delegate?.didReceiveMessage(socket: self.id, message: message) } } - self.receive(on: connection) + + if (connection.state == .ready) { + self.receive(on: connection) + } } } diff --git a/src/definitions.ts b/src/definitions.ts index e7e4e78..13fcd26 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -5,6 +5,7 @@ export interface SocketsPlugin { connect(options: { id: string; }): Promise; send(options: { id: string; message: string }): Promise; disconnect(options: { id: string }): Promise; + close(options: { id: string }): Promise; addListener(eventName: "state", listenerFunc: (message: { id: string; state: string }) => void): Promise; addListener(eventName: "message", listenerFunc: (message: { id: string; message: string }) => void): Promise; diff --git a/src/socket.ts b/src/socket.ts index 880faaf..ac7fb9a 100644 --- a/src/socket.ts +++ b/src/socket.ts @@ -98,7 +98,10 @@ export default class Socket { Sockets.disconnect({ id: this.id, }); + } + close() { + this.disconnect(); SocketManager.getInstance().unregisterSocket(this); }