Files
VibeCoding/webrtc_controller_ios/web_rtc_controller_ios/Signaling/SignalMessage.swift

44 lines
1.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
/// JSON WebRTCSignalServer SignalMessage
/// type: REGISTER / OFFER / ANSWER / ICE_CANDIDATE / TARGET_OFFLINE /
/// CONNECTION_REJECTED / REQUEST_ERROR / REQUEST_TIMEOUT / REGISTER_SUCCESS
struct SignalMessage: Codable {
var type: String?
var fromDeviceId: String?
var toDeviceId: String?
var deviceType: String?
/// payload JSON {"sdp": "..."}
var payload: String?
var authType: String?
var authValue: String?
init(type: String? = nil,
fromDeviceId: String? = nil,
toDeviceId: String? = nil,
deviceType: String? = nil,
payload: String? = nil,
authType: String? = nil,
authValue: String? = nil) {
self.type = type
self.fromDeviceId = fromDeviceId
self.toDeviceId = toDeviceId
self.deviceType = deviceType
self.payload = payload
self.authType = authType
self.authValue = authValue
}
/// payload JSON
func payloadJSON() -> [String: Any]? {
guard let payload, let data = payload.data(using: .utf8) else { return nil }
return (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
}
/// payload
static func encodePayload(_ dict: [String: Any]) -> String? {
guard let data = try? JSONSerialization.data(withJSONObject: dict) else { return nil }
return String(data: data, encoding: .utf8)
}
}