2024-10-03 05:03:39 +00:00
|
|
|
import Foundation
|
|
|
|
import Capacitor
|
|
|
|
|
|
|
|
@objc(SocketsPlugin)
|
|
|
|
public class SocketsPlugin: CAPPlugin, CAPBridgedPlugin {
|
2024-10-03 05:22:10 +00:00
|
|
|
|
|
|
|
private lazy var implementation: Sockets = {
|
|
|
|
return Sockets(plugin: self)
|
|
|
|
}()
|
|
|
|
|
2024-10-03 05:03:39 +00:00
|
|
|
public let identifier = "SocketsPlugin"
|
|
|
|
public let jsName = "Sockets"
|
|
|
|
public let pluginMethods: [CAPPluginMethod] = [
|
2024-10-03 05:22:10 +00:00
|
|
|
CAPPluginMethod(name: "create", returnType: CAPPluginReturnPromise),
|
|
|
|
CAPPluginMethod(name: "connect", returnType: CAPPluginReturnPromise),
|
|
|
|
CAPPluginMethod(name: "send", returnType: CAPPluginReturnPromise),
|
|
|
|
CAPPluginMethod(name: "disconnect", returnType: CAPPluginReturnPromise)
|
2024-10-03 05:03:39 +00:00
|
|
|
]
|
|
|
|
|
2024-10-03 05:22:10 +00:00
|
|
|
@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()
|
2024-10-03 05:03:39 +00:00
|
|
|
}
|
2024-10-03 05:22:10 +00:00
|
|
|
|
|
|
|
@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()
|
|
|
|
}
|
2024-10-03 05:03:39 +00:00
|
|
|
}
|