自定义消息和 provider
1. 自定义消息
1.1 编写自定义普通消息的代码
import { MessageTag, MessageContent, MessageFlag, JsonConverter } from '@rongcloud/imkit';
/**
* 消息标识
* @version 1.0.0
*/
export const CustomTextMessageObjectName = "RCD:TstMsg";
/**
* 自定义文本消息
*
*/
@MessageTag(CustomTextMessageObjectName, MessageFlag.Count)
export class CustomTextMessage extends MessageContent {
// 文本内容
content: string = '';
constructor() {
super();
}
encode(): string {
let map = super.encodeBaseData();
// 将文本内容放到字典
map.set('content', this.content)
return JsonConverter.stringifyFromHashMap(map);
}
decode(contentString: string): void {
let hashMap = JsonConverter.parseToHashMap(contentString);
super.decodeBaseData(hashMap);
// 将文本内容从字典中解出
if (hashMap.get("content")) {
this.content = hashMap.get("content") as string;
}
}
getClassName(): string {
return CustomTextMessage.name
}
}
1.2 编写自定义小灰条消息的代码
/**
* @date 2024/12/16
* @author 融云
*/
import { JsonConverter, MessageContent, MessageFlag, MessageTag, StringChecker } from '@rongcloud/imlib';
export const CustomInfoMessageObjectName = "RCD:InfoMsg";
/**
* 自定义小灰条消息
*/
@MessageTag(CustomInfoMessageObjectName, MessageFlag.Save)
export class CustomInfoMessage extends MessageContent {
/**
* 小灰条内容
*/
message: string = "";
constructor() {
super();
}
encode(): string {
let map = super.encodeBaseData();
if (StringChecker.isValid(this.message)) {
map.set("message", this.message);
}
return JsonConverter.stringifyFromHashMap(map);
}
decode(contentString: string): void {
let map = JsonConverter.parseToHashMap(contentString);
super.decodeBaseData(map);
if (map.get("message")) {
this.message = map.get("message") as string;
}
}
getClassName(): string {
return CustomInfoMessage.name;
}
}