import 'dart:convert'; import 'dart:typed_data'; /// MobileIMSDK 官方 TCP 帧:4 字节大端长度 + UTF-8 JSON Protocal。 class ClientType { static const login = 0; static const keepAlive = 1; static const commonData = 2; static const logout = 3; static const received = 4; static const echo = 5; } class ServerType { static const login = 50; static const keepAlive = 51; static const error = 52; static const echo = 53; static const kickout = 54; } class Protocal { Protocal({ required this.type, this.dataContent = '', this.from = '0', this.to = '0', this.fp, this.qos = false, this.typeu = -1, this.sm = -1, }); int type; String dataContent; String from; String to; String? fp; bool qos; int typeu; int sm; Map toJson() => { 'bridge': false, 'type': type, 'dataContent': dataContent, 'from': from, 'to': to, 'fp': fp, 'QoS': qos, 'typeu': typeu, 'sm': sm, }; factory Protocal.fromJson(Map j) => Protocal( type: (j['type'] as num?)?.toInt() ?? 0, dataContent: '${j['dataContent'] ?? ''}', from: '${j['from'] ?? '0'}', to: '${j['to'] ?? '0'}', fp: j['fp']?.toString(), qos: j['QoS'] == true || j['qos'] == true, typeu: (j['typeu'] as num?)?.toInt() ?? -1, sm: (j['sm'] as num?)?.toInt() ?? -1, ); } Uint8List encodeFrame(Protocal p) { final body = utf8.encode(jsonEncode(p.toJson())); final header = ByteData(4)..setUint32(0, body.length); return Uint8List.fromList([...header.buffer.asUint8List(), ...body]); } const maxBody = 6 * 1024;