62 lines
1.8 KiB
Dart
62 lines
1.8 KiB
Dart
import 'dart:convert';
|
||
|
||
/// 信令消息模型,对应 Android 端的 SignalMessage。
|
||
///
|
||
/// 字段含义:
|
||
/// - [type] 消息类型:REGISTER / OFFER / ANSWER / ICE_CANDIDATE
|
||
/// - [fromDeviceId] 发送方设备 ID
|
||
/// - [toDeviceId] 接收方设备 ID
|
||
/// - [deviceType] 设备类型:CONTROLLER / CONTROLLED
|
||
/// - [payload] JSON 字符串形式的负载(SDP / ICE 候选等)
|
||
class SignalMessage {
|
||
final String? type;
|
||
final String? fromDeviceId;
|
||
final String? toDeviceId;
|
||
final String? deviceType;
|
||
final String? payload;
|
||
|
||
const SignalMessage({
|
||
this.type,
|
||
this.fromDeviceId,
|
||
this.toDeviceId,
|
||
this.deviceType,
|
||
this.payload,
|
||
});
|
||
|
||
factory SignalMessage.fromJson(Map<String, dynamic> json) => SignalMessage(
|
||
type: json['type'] as String?,
|
||
fromDeviceId: json['fromDeviceId'] as String?,
|
||
toDeviceId: json['toDeviceId'] as String?,
|
||
deviceType: json['deviceType'] as String?,
|
||
payload: json['payload'] as String?,
|
||
);
|
||
|
||
Map<String, dynamic> toJson() => {
|
||
if (type != null) 'type': type,
|
||
if (fromDeviceId != null) 'fromDeviceId': fromDeviceId,
|
||
if (toDeviceId != null) 'toDeviceId': toDeviceId,
|
||
if (deviceType != null) 'deviceType': deviceType,
|
||
if (payload != null) 'payload': payload,
|
||
};
|
||
|
||
/// 便捷构造方法:payload 为任意 Map,会自动序列化为 JSON 字符串。
|
||
factory SignalMessage.withPayload({
|
||
required String type,
|
||
required String fromDeviceId,
|
||
required String toDeviceId,
|
||
required String deviceType,
|
||
required Map<String, dynamic> payload,
|
||
}) {
|
||
return SignalMessage(
|
||
type: type,
|
||
fromDeviceId: fromDeviceId,
|
||
toDeviceId: toDeviceId,
|
||
deviceType: deviceType,
|
||
payload: jsonEncode(payload),
|
||
);
|
||
}
|
||
|
||
@override
|
||
String toString() => jsonEncode(toJson());
|
||
}
|