app-starlock/lib/talk/startChart/entity/heartbeat_response.dart

39 lines
1.1 KiB
Dart
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.

class HeartbeatResponse {
int statusCode; // 状态码1字节无符号
int nextPingTime; // 下次ping时间秒数2字节无符号
HeartbeatResponse({required this.statusCode, required this.nextPingTime});
@override
String toString() {
return 'HeartbeatResponse{statusCode: $statusCode, nextPingTime: $nextPingTime}';
}
// 反序列化方法
static HeartbeatResponse deserialize(List<int> bytes) {
if (bytes.length < 3) {
throw FormatException("Invalid HeartbeatResponse length");
}
final response = HeartbeatResponse(
statusCode: bytes[0], // 状态码1字节
nextPingTime: (bytes[2] << 8) | bytes[1], // 下次ping时间2字节小端序
);
return response;
}
// 序列化方法
static List<int> serialize(HeartbeatResponse response) {
final List<int> bytes = [];
// 序列化状态码
bytes.add(response.statusCode);
// 序列化下次ping时间2字节小端序
bytes.add(response.nextPingTime & 0xFF);
bytes.add((response.nextPingTime >> 8) & 0xFF);
return bytes;
}
}