This commit is contained in:
eskimo
2024-10-03 01:22:10 -04:00
parent 6a108e8f23
commit de6b28ab43
3 changed files with 190 additions and 15 deletions

View File

@@ -1,23 +1,50 @@
import Foundation
import Capacitor
/**
* Please read the Capacitor iOS Plugin Development Guide
* here: https://capacitorjs.com/docs/plugins/ios
*/
@objc(SocketsPlugin)
public class SocketsPlugin: CAPPlugin, CAPBridgedPlugin {
private lazy var implementation: Sockets = {
return Sockets(plugin: self)
}()
public let identifier = "SocketsPlugin"
public let jsName = "Sockets"
public let pluginMethods: [CAPPluginMethod] = [
CAPPluginMethod(name: "echo", returnType: CAPPluginReturnPromise)
CAPPluginMethod(name: "create", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "connect", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "send", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "disconnect", returnType: CAPPluginReturnPromise)
]
private let implementation = Sockets()
@objc func echo(_ call: CAPPluginCall) {
let value = call.getString("value") ?? ""
call.resolve([
"value": implementation.echo(value)
])
@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)
call.resolve()
}
@objc func connect(_ call: CAPPluginCall) {
let id = call.getString("id") ?? ""
implementation.connect(id: id)
call.resolve()
}
@objc func send(_ call: CAPPluginCall) {
let id = call.getString("id") ?? ""
let message = call.getString("message") ?? ""
implementation.send(id: id, message: message)
call.resolve()
}
@objc func disconnect(_ call: CAPPluginCall) {
let id = call.getString("id") ?? ""
implementation.disconnect(id: id)
call.resolve()
}
}