// rtm 实时消息封装 const RTM = { // 初始化 init: async () => { // 初始化回调 await rtmModule.setCallBack(res => { RTM.callBack(res) }) // 初始化实例 await rtmModule.createInstance({ "appId": Config.RTM_APPID }, res => { console.log(res); }) // 登录 RTM 系统 await rtmModule.login({ "token": "", "userId": Config.uid }, (res) => { }) // 使用 RTM 呼叫邀请(设置邀请呼叫实例的监听器) await rtmModule.setCallEventListener() }, // 回调 callBack: (res) => { switch (res.rtmEvent) { // SDK 与 RTM 系统的连接状态发生改变回调。 case 'onConnectionStateChanged': console.log(res.state, res.reason) break; // 收到点对点消息回调 case 'onPeerMessageReceived': console.log(res.text, res.peerId) break; // 返回给主叫:被叫已接受呼叫邀请 case 'onLocalInvitationAccepted': console.log(res) break; // 返回给主叫:呼叫邀请已被取消 case 'onLocalInvitationCanceled': console.log(res) break; // 返回给主叫:呼叫邀请进程失败 case 'onLocalInvitationFailure': console.log(res) break; // 返回给主叫:被叫已收到呼叫邀请 case 'onLocalInvitationReceivedByPeer': console.log(res) break; // 返回给主叫:被叫已拒绝呼叫邀请 case 'onLocalInvitationRefused': console.log(res) break; // 返回给被叫:接受呼叫邀请成功 case 'onRemoteInvitationAccepted': console.log(res) break; // 返回给被叫:主叫已取消呼叫邀请 case 'onRemoteInvitationCanceled': console.log(res) break; // 返回给被叫:来自主叫的呼叫邀请进程失败 case 'onRemoteInvitationFailure': console.log(res) break; // 返回给被叫:收到一个呼叫邀请 case 'onRemoteInvitationReceived': console.log(res) break; // 返回给被叫:拒绝呼叫邀请成功 case 'onRemoteInvitationRefused': console.log(res) break; default: break; } // console.log(res); }, // 查询所有用户是否在线 queryPeersOnlineStatus: async function(peerIdLits) { return await new Promise((resolve, reject) => { rtmModule.queryPeersOnlineStatus({ "peerIds": peerIdLits }, (res) => { resolve(res); }) }) }, // 接受来自对方的呼叫邀请 acceptRemoteInvitation: function(calleeId, info = "") { console.log('接受来自对方的呼叫邀请'); rtmModule.acceptRemoteInvitation({ "calleeId": calleeId, // 供被叫获取主叫的用户 ID "response": info ? JSON.stringify(info) : "" // 邀请响应 }, (res) => { console.log(res.code === 0 ? '' : "调用 acceptRemoteInvitation 接受来自对方的呼叫邀请失败"); }) }, // 拒绝来自对方的呼叫邀请 refuseRemoteInvitation: function(userId, info = "") { rtmModule.refuseRemoteInvitation({ "calleeId": userId, "response": info ? JSON.stringify(info) : "" // 邀请内容 }, (res) => { console.log(res.code === 0 ? "" : "调用 cancelLocalInvitation 取消呼叫失败"); }); }, // 取消给对方的呼叫邀请 cancelLocalInvitation: function(calleeId, info = "") { rtmModule.cancelLocalInvitation({ "calleeId": calleeId, // 被呼叫者的 user ID "content": info ? JSON.stringify(info) : "" // 邀请内容 }, (res) => { console.log(res.code === 0 ? "" : "调用 cancelLocalInvitation 取消呼叫失败"); }) }
评论