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
|
2024-10-03 18:42:36 +00:00
|
|
|
let socket = implementation.create(id: id)
|
2024-10-03 05:22:10 +00:00
|
|
|
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") ?? ""
|
2024-10-03 18:42:36 +00:00
|
|
|
let host = call.getString("host") ?? ""
|
|
|
|
let port = call.getInt("port") ?? 0
|
|
|
|
let useTLS = call.getBool("useTLS") ?? false
|
|
|
|
let acceptInvalidCertificates = call.getBool("acceptInvalidCertificates") ?? false
|
|
|
|
implementation.connect(id: id, host: host, port: port, useTLS: useTLS, acceptInvalidCertificates: acceptInvalidCertificates)
|
2024-10-03 05:22:10 +00:00
|
|
|
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
|
|
|
}
|