123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import * as RongIMLib from '@rongcloud/imlib-v4'
- import config from '@/config';
- let appkey=config.RongYun.appkey;
- const im = RongIMLib.init({ appkey});
- export function rongcloudInit(params){
- im.watch({
- // 监听会话列表变更事件
- conversation (event) {
- if(params.conversation){
- params.conversation(event)
- }
- },
- // 监听消息通知
- message (event) {
- const message = event.message;
- console.log('收到消息'+message)
- if(params.receiveMsg){
- params.receiveMsg(message)
- }
- },
- // 监听 IM 连接状态变化
- status (event) {
- console.log('监听 IM 连接状态变化connection status:', event.status);
- if(params.status){
- params.status(event)
- }
- },
- // 监听聊天室 KV 数据变更
- chatroom (event) {
- /**
- * 聊天室 KV 存储数据更新
- * @example
- * [
- * {
- * 'key': 'name',
- * 'value': '我是小融融',
- * 'timestamp': 1597591258338,
- * 'chatroomId': 'z002',
- * 'type': 1 // 1: 更新( 含:修改和新增 )、2: 删除
- * },
- * ]
- */
- const updatedEntries = event.updatedEntries
- if(params.updatedEntries){
- params.updatedEntries(updatedEntries)
- }
- },
- expansion (event) {
- /**
- * 更新的消息拓展数据
- * @example {
- * expansion: { key: 'value' }, // 设置或更新的扩展值
- * messageUId: 'URIT-URIT-ODMF-DURR' // 设置或更新扩展的消息 uid
- * }
- */
- //const updatedExpansion = event.updatedExpansion;
- /**
- * 删除的消息拓展数据
- * @example {
- * deletedKeys: ['key1', 'key2'], // 设置或更新的扩展值
- * messageUId: 'URIT-URIT-ODMF-DURR' // 设置或更新扩展的消息 uid
- * }
- */
- const deletedExpansion = event.deletedExpansion;
- if(params.deletedExpansion){
- params.deletedExpansion(deletedExpansion)
- }
- }
- });
- }
- export function connect({token}) {
- im.connect({ token}).then(user => {
- console.log('链接成功, 链接用户 id 为: ', user.id);
- }).catch(error => {
- console.log('链接失败: ', error.code, error.msg);
- });
- }
- // 向会话内发消息
- export function sendMsg({sendmessage,targetId}) {
- console.log('向会话内发消息')
- return new Promise(function(resolve,reject){
- const conversation = im.Conversation.get({
- // targetId
- targetId,
- // 会话类型:RongIMLib.CONVERSATION_TYPE.PRIVATE | RongIMLib.CONVERSATION_TYPE.GROUP
- type: RongIMLib.CONVERSATION_TYPE.PRIVATE
- });
- conversation.send({
- // 消息类型,其中 RongIMLib.MESSAGE_TYPE 为 IMLib 内部的内置消息类型常量定义
- messageType: RongIMLib.MESSAGE_TYPE.TEXT, // 'RC:TxtMsg'
- // 消息内容
- content: {
- content:sendmessage // 文本内容
- }
- }).then(function(message){
- console.log('发送文字成功', message);
- resolve(message)
- }).catch(error => {
- reject(error)
- console.log('发送文字消息失败', error.code, error.msg);
- });
- })
- }
|