tls support

This commit is contained in:
eskimo 2024-10-03 13:55:00 -04:00
parent 7e77833452
commit 3280dfaa58
4 changed files with 38 additions and 4 deletions

View File

@ -17,6 +17,7 @@ npx cap sync
* [`connect(...)`](#connect) * [`connect(...)`](#connect)
* [`send(...)`](#send) * [`send(...)`](#send)
* [`disconnect(...)`](#disconnect) * [`disconnect(...)`](#disconnect)
* [`close(...)`](#close)
* [`addListener('state', ...)`](#addlistenerstate-) * [`addListener('state', ...)`](#addlistenerstate-)
* [`addListener('message', ...)`](#addlistenermessage-) * [`addListener('message', ...)`](#addlistenermessage-)
* [Interfaces](#interfaces) * [Interfaces](#interfaces)
@ -78,10 +79,23 @@ disconnect(options: { id: string; }) => Promise<void>
-------------------- --------------------
### close(...)
```typescript
close(options: { id: string; }) => Promise<void>
```
| Param | Type |
| ------------- | ---------------------------- |
| **`options`** | <code>{ id: string; }</code> |
--------------------
### addListener('state', ...) ### addListener('state', ...)
```typescript ```typescript
addListener(eventName: 'state', listenerFunc: (message: { id: string; state: string; }) => void) => Promise<PluginListenerHandle> addListener(eventName: "state", listenerFunc: (message: { id: string; state: string; }) => void) => Promise<PluginListenerHandle>
``` ```
| Param | Type | | Param | Type |
@ -97,7 +111,7 @@ addListener(eventName: 'state', listenerFunc: (message: { id: string; state: str
### addListener('message', ...) ### addListener('message', ...)
```typescript ```typescript
addListener(eventName: 'message', listenerFunc: (message: { id: string; message: string; }) => void) => Promise<PluginListenerHandle> addListener(eventName: "message", listenerFunc: (message: { id: string; message: string; }) => void) => Promise<PluginListenerHandle>
``` ```
| Param | Type | | Param | Type |

View File

@ -28,7 +28,20 @@ public class Socket: NSObject {
} }
public func connect() { 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:) connection.stateUpdateHandler = self.stateDidChange(to:)
self.receive(on: connection) self.receive(on: connection)
connection.start(queue: .main) connection.start(queue: .main)
@ -84,8 +97,11 @@ public class Socket: NSObject {
self.delegate?.didReceiveMessage(socket: self.id, message: message) self.delegate?.didReceiveMessage(socket: self.id, message: message)
} }
} }
if (connection.state == .ready) {
self.receive(on: connection) self.receive(on: connection)
} }
} }
}
} }

View File

@ -5,6 +5,7 @@ export interface SocketsPlugin {
connect(options: { id: string; }): Promise<void>; connect(options: { id: string; }): Promise<void>;
send(options: { id: string; message: string }): Promise<void>; send(options: { id: string; message: string }): Promise<void>;
disconnect(options: { id: string }): Promise<void>; disconnect(options: { id: string }): Promise<void>;
close(options: { id: string }): Promise<void>;
addListener(eventName: "state", listenerFunc: (message: { id: string; state: string }) => void): Promise<PluginListenerHandle>; addListener(eventName: "state", listenerFunc: (message: { id: string; state: string }) => void): Promise<PluginListenerHandle>;
addListener(eventName: "message", listenerFunc: (message: { id: string; message: string }) => void): Promise<PluginListenerHandle>; addListener(eventName: "message", listenerFunc: (message: { id: string; message: string }) => void): Promise<PluginListenerHandle>;

View File

@ -98,7 +98,10 @@ export default class Socket {
Sockets.disconnect({ Sockets.disconnect({
id: this.id, id: this.id,
}); });
}
close() {
this.disconnect();
SocketManager.getInstance().unregisterSocket(this); SocketManager.getInstance().unregisterSocket(this);
} }