shishuangxi

集成融云库

This file is too large to display.
... ... @@ -10,6 +10,7 @@
"wdKit": "file:../../commons/wdKit",
"premierlibrary": "file:./libs/premierlibrary.har",
"wdTracking": "file:../wdTracking",
"wdBean": "file:../../features/wdBean"
"wdBean": "file:../../features/wdBean",
"@rongcloud/imlib": "file:./libs/RongIMLib.har" // 该配置手动依赖
}
}
... ...
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;
}
}
\ No newline at end of file
... ...
import {
ConversationIdentifier,
ConversationType,
IAsyncResult,
IMEngine,
InitOption,
ISendMsgOption,
Message,
ReceivedInfo
} from '@rongcloud/imlib';
import { Context } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { CustomMessageContent } from './CustomMessageContent';
export class IMHelper {
private static appKey: string = "c9kqb3rdcfolj"
/**初始化*/
static initSdk(context: Context) {
let initOption = new InitOption();
let appKey = IMHelper.appKey;
IMEngine.getInstance().init(context, appKey, initOption);
}
/**注册消息监听*/
static registerMsgListener() {
IMEngine.getInstance().setMessageReceivedListener((message: Message, info: ReceivedInfo) => {
// 针对接收离线消息时,服务端会将 200 条消息打成一个包发到客户端,客户端对这包数据进行解析。该参数表示每个数据包数据逐条上抛后,还剩余的条数
let left = info.left;
// 消息是否离线消息
let isOffline = info.isOffline;
// 是否在服务端还存在未下发的消息包
let hasPackage = info.hasPackage;
hilog.info(0x0000, 'IM-App', 'message: ' + message + "//" + info.hasPackage);
});
}
/**建立 IM 连接*/
static creatSocket(iMToken: string) {
// let token = "m8KmlHGuI4wjGBr2sHaUAASYiD7nx6VZJ5vkHoVrMhU=@b20a.cn.rongnav.com;b20a.cn.rongcfg.com";
// iMToken = "m8KmlHGuI4zGTdp+DIN9qwSYiD7nx6VZmZKqed9uqUk=@b20a.cn.rongnav.com;b20a.cn.rongcfg.com";
let timeout = 5;
hilog.info(0x0000, 'IM-App', 'connect token:%{public}s', iMToken);
IMEngine.getInstance().connect(iMToken, timeout)
.then(result => {
hilog.info(0x0000, 'IM-App', 'connect result :%{public}s', JSON.stringify(result));
});
}
/**发送消息*/
static sendMsg(type: ConversationType, targetId: string, data: CustomMessageContent): Promise<IAsyncResult<Message>> {
let conId = new ConversationIdentifier();
conId.conversationType = type;
conId.targetId = targetId; // 按需填写实际的会话 id
let option: ISendMsgOption = {};
// 创建消息体
let msg = new Message(conId, data);
// IMEngine.getInstance().sendMessage(msg,option,(msg: Message) => {
// // 消息入库回调
// hilog.info(0x0000, 'IM-App', 'SendTextMessage onSave msg:%{public}s', JSON.stringify(msg));
// }).then(result => {
// // 消息发送结果
// hilog.info(0x0000, 'IM-App', 'SendTextMessage onResult :%{public}s', JSON.stringify(result));
// })
return IMEngine.getInstance().sendMessage(msg, option, () => {
});
}
}
\ No newline at end of file
... ...
... ... @@ -15,7 +15,8 @@
"@mpaas/udid": "0.0.2",
"@mpaas/upgrade": "0.0.2",
"@mpaas/framework": "0.0.2",
"@ohos/imageknife": "^2.1.2"
"@ohos/imageknife": "^2.1.2",
"@rongcloud/imlib": "file:features/wdPlayer/libs/RongIMLib.har"
},
"dynamicDependencies": {}
}
\ No newline at end of file
... ...