capacitor-sockets/ios/Sources/SocketsPlugin/SocketsPlugin.swift
2024-10-03 01:22:10 -04:00

51 lines
1.6 KiB
Swift

import Foundation
import Capacitor
@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: "create", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "connect", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "send", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "disconnect", returnType: CAPPluginReturnPromise)
]
@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()
}
}