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)
* [`send(...)`](#send)
* [`disconnect(...)`](#disconnect)
* [`close(...)`](#close)
* [`addListener('state', ...)`](#addlistenerstate-)
* [`addListener('message', ...)`](#addlistenermessage-)
* [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', ...)
```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 |
@ -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<PluginListenerHandle>
addListener(eventName: "message", listenerFunc: (message: { id: string; message: string; }) => void) => Promise<PluginListenerHandle>
```
| Param | Type |

View File

@ -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)
}
}
}

View File

@ -5,6 +5,7 @@ export interface SocketsPlugin {
connect(options: { id: string; }): Promise<void>;
send(options: { id: string; message: 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: "message", listenerFunc: (message: { id: string; message: string }) => void): Promise<PluginListenerHandle>;

View File

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