Showing
5 changed files
with
115 additions
and
2 deletions
This file is too large to display.
| @@ -10,6 +10,7 @@ | @@ -10,6 +10,7 @@ | ||
| 10 | "wdKit": "file:../../commons/wdKit", | 10 | "wdKit": "file:../../commons/wdKit", |
| 11 | "premierlibrary": "file:./libs/premierlibrary.har", | 11 | "premierlibrary": "file:./libs/premierlibrary.har", |
| 12 | "wdTracking": "file:../wdTracking", | 12 | "wdTracking": "file:../wdTracking", |
| 13 | - "wdBean": "file:../../features/wdBean" | 13 | + "wdBean": "file:../../features/wdBean", |
| 14 | + "@rongcloud/imlib": "file:./libs/RongIMLib.har" // 该配置手动依赖 | ||
| 14 | } | 15 | } |
| 15 | } | 16 | } |
| 1 | +import { JsonUtil, MessageContent, MessageFlag, MessageTag } from '@rongcloud/imlib'; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +const CustomMessageContentObjectName = "App:CustomMsg"; | ||
| 5 | +const CustomMessageContentFlag = MessageFlag.Count; | ||
| 6 | + | ||
| 7 | +@MessageTag(CustomMessageContentObjectName,CustomMessageContentFlag) | ||
| 8 | +export class CustomMessageContent extends MessageContent { | ||
| 9 | + type: string = "" //消息类型 | ||
| 10 | + data: string = "" //消息体 json字符串 | ||
| 11 | + constructor() { | ||
| 12 | + super() | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + encode(): string { | ||
| 16 | + let map = super.encodeBaseData(); | ||
| 17 | + map.set("type", this.type as Object) | ||
| 18 | + map.set("data", this.data as Object) | ||
| 19 | + return JsonUtil.stringifyFromMap(map); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + decode(contentJson: string): void { | ||
| 23 | + // 5.1 将字符串转为 map | ||
| 24 | + let map = JsonUtil.parseToMap(contentJson); | ||
| 25 | + // 5.2 将基类的数据解析出来 | ||
| 26 | + super.decodeBaseData(map); | ||
| 27 | + | ||
| 28 | + // 5.3 将本类的独有属性解析 | ||
| 29 | + // 说明:需要将 Object 转为对应的数据类型 | ||
| 30 | + this.type = map.get("type") as string; | ||
| 31 | + this.data = map.get("data") as string; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + getClassName(): string { | ||
| 35 | + return CustomMessageContent.name; | ||
| 36 | + } | ||
| 37 | +} |
| 1 | +import { | ||
| 2 | + ConversationIdentifier, | ||
| 3 | + ConversationType, | ||
| 4 | + IAsyncResult, | ||
| 5 | + IMEngine, | ||
| 6 | + InitOption, | ||
| 7 | + ISendMsgOption, | ||
| 8 | + Message, | ||
| 9 | + ReceivedInfo | ||
| 10 | +} from '@rongcloud/imlib'; | ||
| 11 | +import { Context } from '@kit.AbilityKit'; | ||
| 12 | +import { hilog } from '@kit.PerformanceAnalysisKit'; | ||
| 13 | +import { CustomMessageContent } from './CustomMessageContent'; | ||
| 14 | + | ||
| 15 | +export class IMHelper { | ||
| 16 | + private static appKey: string = "c9kqb3rdcfolj" | ||
| 17 | + | ||
| 18 | + /**初始化*/ | ||
| 19 | + static initSdk(context: Context) { | ||
| 20 | + let initOption = new InitOption(); | ||
| 21 | + let appKey = IMHelper.appKey; | ||
| 22 | + IMEngine.getInstance().init(context, appKey, initOption); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + /**注册消息监听*/ | ||
| 26 | + static registerMsgListener() { | ||
| 27 | + IMEngine.getInstance().setMessageReceivedListener((message: Message, info: ReceivedInfo) => { | ||
| 28 | + // 针对接收离线消息时,服务端会将 200 条消息打成一个包发到客户端,客户端对这包数据进行解析。该参数表示每个数据包数据逐条上抛后,还剩余的条数 | ||
| 29 | + let left = info.left; | ||
| 30 | + // 消息是否离线消息 | ||
| 31 | + let isOffline = info.isOffline; | ||
| 32 | + // 是否在服务端还存在未下发的消息包 | ||
| 33 | + let hasPackage = info.hasPackage; | ||
| 34 | + hilog.info(0x0000, 'IM-App', 'message: ' + message + "//" + info.hasPackage); | ||
| 35 | + }); | ||
| 36 | + | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /**建立 IM 连接*/ | ||
| 40 | + static creatSocket(iMToken: string) { | ||
| 41 | + // let token = "m8KmlHGuI4wjGBr2sHaUAASYiD7nx6VZJ5vkHoVrMhU=@b20a.cn.rongnav.com;b20a.cn.rongcfg.com"; | ||
| 42 | + // iMToken = "m8KmlHGuI4zGTdp+DIN9qwSYiD7nx6VZmZKqed9uqUk=@b20a.cn.rongnav.com;b20a.cn.rongcfg.com"; | ||
| 43 | + let timeout = 5; | ||
| 44 | + | ||
| 45 | + hilog.info(0x0000, 'IM-App', 'connect token:%{public}s', iMToken); | ||
| 46 | + IMEngine.getInstance().connect(iMToken, timeout) | ||
| 47 | + .then(result => { | ||
| 48 | + hilog.info(0x0000, 'IM-App', 'connect result :%{public}s', JSON.stringify(result)); | ||
| 49 | + }); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /**发送消息*/ | ||
| 53 | + static sendMsg(type: ConversationType, targetId: string, data: CustomMessageContent): Promise<IAsyncResult<Message>> { | ||
| 54 | + let conId = new ConversationIdentifier(); | ||
| 55 | + conId.conversationType = type; | ||
| 56 | + conId.targetId = targetId; // 按需填写实际的会话 id | ||
| 57 | + | ||
| 58 | + let option: ISendMsgOption = {}; | ||
| 59 | + | ||
| 60 | + // 创建消息体 | ||
| 61 | + let msg = new Message(conId, data); | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + // IMEngine.getInstance().sendMessage(msg,option,(msg: Message) => { | ||
| 65 | + // // 消息入库回调 | ||
| 66 | + // hilog.info(0x0000, 'IM-App', 'SendTextMessage onSave msg:%{public}s', JSON.stringify(msg)); | ||
| 67 | + // }).then(result => { | ||
| 68 | + // // 消息发送结果 | ||
| 69 | + // hilog.info(0x0000, 'IM-App', 'SendTextMessage onResult :%{public}s', JSON.stringify(result)); | ||
| 70 | + // }) | ||
| 71 | + return IMEngine.getInstance().sendMessage(msg, option, () => { | ||
| 72 | + }); | ||
| 73 | + } | ||
| 74 | +} |
| @@ -15,7 +15,8 @@ | @@ -15,7 +15,8 @@ | ||
| 15 | "@mpaas/udid": "0.0.2", | 15 | "@mpaas/udid": "0.0.2", |
| 16 | "@mpaas/upgrade": "0.0.2", | 16 | "@mpaas/upgrade": "0.0.2", |
| 17 | "@mpaas/framework": "0.0.2", | 17 | "@mpaas/framework": "0.0.2", |
| 18 | - "@ohos/imageknife": "^2.1.2" | 18 | + "@ohos/imageknife": "^2.1.2", |
| 19 | + "@rongcloud/imlib": "file:features/wdPlayer/libs/RongIMLib.har" | ||
| 19 | }, | 20 | }, |
| 20 | "dynamicDependencies": {} | 21 | "dynamicDependencies": {} |
| 21 | } | 22 | } |
-
Please register or login to post a comment