CustomMessageContent.ets
1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { JsonUtil, MessageContent, MessageFlag, MessageTag } from '@rongcloud/imlib';
const CustomMessageContentObjectName = "App:CustomMsg";
const CustomMessageContentFlag = MessageFlag.Count;
@MessageTag(CustomMessageContentObjectName,CustomMessageContentFlag)
export class CustomMessageContent extends MessageContent {
type: string = "" //消息类型
data: string = "" //消息体 json字符串
constructor() {
super()
}
encode(): string {
let map = super.encodeBaseData();
map.set("type", this.type as Object)
map.set("data", this.data as Object)
return JsonUtil.stringifyFromMap(map);
}
decode(contentJson: string): void {
// 5.1 将字符串转为 map
let map = JsonUtil.parseToMap(contentJson);
// 5.2 将基类的数据解析出来
super.decodeBaseData(map);
// 5.3 将本类的独有属性解析
// 说明:需要将 Object 转为对应的数据类型
this.type = map.get("type") as string;
this.data = map.get("data") as string;
}
getClassName(): string {
return CustomMessageContent.name;
}
}