|
@@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
|
|
|
import { HttpService } from './http.service';
|
|
|
import { LiveService } from './live.service';
|
|
|
import * as Parse from 'parse';
|
|
|
+import { AlertController, ToastController } from '@ionic/angular';
|
|
|
// import AgoraRTM from 'agora-rtmClient';
|
|
|
declare const AgoraRTM: any;
|
|
|
@Injectable({
|
|
@@ -19,9 +20,13 @@ export class MessageService {
|
|
|
userId: string = Parse.User.current()?.id!;
|
|
|
|
|
|
pageFun?: Function; //页面传入的方法
|
|
|
- constructor(private liveService: LiveService, private http: HttpService) {
|
|
|
- // this.initRTM(this.msChannelName);
|
|
|
- }
|
|
|
+ alert: any; // 弹窗
|
|
|
+ constructor(
|
|
|
+ private liveService: LiveService,
|
|
|
+ private alertController: AlertController,
|
|
|
+ public toastController: ToastController,
|
|
|
+ private http: HttpService
|
|
|
+ ) {}
|
|
|
messageMapList: any = {
|
|
|
global_room: [
|
|
|
// 世界频道消息列表
|
|
@@ -80,6 +85,7 @@ export class MessageService {
|
|
|
|
|
|
/* 获取token */
|
|
|
async getToken(channel?: string) {
|
|
|
+ this.userId = Parse.User.current()?.id!;
|
|
|
//获取频道token记录
|
|
|
let uid = Parse.User.current()?.id;
|
|
|
let baseurl = 'https://server.fmode.cn/api/webrtc/build_token';
|
|
@@ -102,34 +108,75 @@ export class MessageService {
|
|
|
|
|
|
async initRTM(channelName: string) {
|
|
|
// let states = ['CONNECTED', 'CONNECTING'];
|
|
|
- //已连接,无需重复连接
|
|
|
- // if (
|
|
|
- // this.rtmClientMap?.[channelName]?.connectState &&
|
|
|
- // states.includes(this.rtmClientMap[channelName].connectState)
|
|
|
- // )
|
|
|
- // return;
|
|
|
- // console.log('initRTM');
|
|
|
-
|
|
|
if (this.options.connectState) return;
|
|
|
await this.getToken(channelName);
|
|
|
+ // const rtmConfig = { logLevel: 'debug' };
|
|
|
this.rtmClient = new AgoraRTM.RTM(this.appid, this.userId);
|
|
|
- // this.rtmClientMap[channelName] = {
|
|
|
- // rtmClient: new AgoraRTM.RTM(this.appid, this.userId),
|
|
|
- // };
|
|
|
this.joinReady();
|
|
|
await this.loginRTM();
|
|
|
// this.subscribeMessage(channelName);
|
|
|
- this.publishMessage('user-online',channelName,'USER')//用户上线通知
|
|
|
+ // this.publishMessage('user-online', channelName, 'USER'); //用户上线通知
|
|
|
}
|
|
|
- /* 监听频道消息 */
|
|
|
+ /**@监听频道消息
|
|
|
+ *@'USERCALLINVITATION': 用户通话邀请
|
|
|
+ *@'REFUSE': 拒绝
|
|
|
+ *@'CLOASE': 拒绝
|
|
|
+ */
|
|
|
joinReady(channelName?: string) {
|
|
|
- this.rtmClient.addEventListener('message', (event: any) => {
|
|
|
- console.log('message', event);
|
|
|
+ this.rtmClient.addEventListener('message', async (event: any) => {
|
|
|
+ console.log('接收到一条消息:', event);
|
|
|
+ const message = JSON.parse(event.message);
|
|
|
+ let is_self = event.publisher == this.userId;
|
|
|
+ let userData = await this.getUserMetadata(event.publisher);
|
|
|
+ if (
|
|
|
+ event.channelName == this.userId &&
|
|
|
+ message.text == 'USERCALLINVITATION' &&
|
|
|
+ !is_self
|
|
|
+ ) {
|
|
|
+ if (this.alert) return;
|
|
|
+ console.log(`收到${userData.nickname.value ?? '未知用户'}通话邀请`);
|
|
|
+ this.alert = await this.alertController.create({
|
|
|
+ cssClass: 'my-custom-class',
|
|
|
+ header: '通话邀请',
|
|
|
+ message: `收到${userData.nickname.value ?? '未知用户'}通话邀请`,
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ text: '拒绝',
|
|
|
+ role: 'cancel',
|
|
|
+ handler: (blah) => {
|
|
|
+ this.publishMessage('REFUSE', event.channelName);
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '接受',
|
|
|
+ cssClass: 'secondary',
|
|
|
+ handler: () => {
|
|
|
+ // this.sendVideoCallInvite();
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ });
|
|
|
+ await this.alert.present();
|
|
|
+ return;
|
|
|
+ } else if (
|
|
|
+ event.channelName == this.userId &&
|
|
|
+ message.text == 'CLOASE' &&
|
|
|
+ !is_self
|
|
|
+ ) {
|
|
|
+ console.log(`${userData.nickname.value ?? '未知用户'}取消通话`);
|
|
|
+ this.alert?.dismiss();
|
|
|
+ const toast = await this.toastController.create({
|
|
|
+ message: '对方已取消视频通话邀请',
|
|
|
+ color: 'warning',
|
|
|
+ duration: 1500,
|
|
|
+ });
|
|
|
+ toast.present();
|
|
|
+ return;
|
|
|
+ }
|
|
|
this.showMessage(event);
|
|
|
});
|
|
|
this.rtmClient.addEventListener('presence', (event: any) => {
|
|
|
console.log('频道人员状态变化 ', event);
|
|
|
- this.pageFun?.();
|
|
|
});
|
|
|
this.rtmClient.addEventListener('linkState', (event: any) => {
|
|
|
console.log('连接状态: ', event);
|
|
@@ -199,23 +246,28 @@ export class MessageService {
|
|
|
/* 订阅消息 */
|
|
|
subscribeMessage(channelName: string, param?: any, callback?: any) {
|
|
|
if (this.channelNameList[channelName]) return;
|
|
|
- const options = {
|
|
|
- withMessage: param?.message ?? false, // message 事件
|
|
|
- withPresence: false, // presence 事件
|
|
|
- beQuiet: false, // quiet 事件
|
|
|
- withMetadata: false, // metadata 事件
|
|
|
- withLock: false, // lock 事件
|
|
|
- };
|
|
|
- this.rtmClient
|
|
|
- .subscribe(channelName, options)
|
|
|
- .then((res: any) => {
|
|
|
- console.log('subscribeMessage', res);
|
|
|
- //订阅成功
|
|
|
- this.channelNameList[channelName] = true;
|
|
|
- })
|
|
|
- .catch((err: any) => {
|
|
|
- console.error('subscribeMessageErr', err);
|
|
|
- });
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const options = {
|
|
|
+ withMessage: param?.message ?? false, // message 事件
|
|
|
+ withPresence: param?.presence ?? false, // presence 事件
|
|
|
+ beQuiet: false, // quiet 事件
|
|
|
+ withMetadata: false, // metadata 事件
|
|
|
+ withLock: false, // lock 事件
|
|
|
+ };
|
|
|
+ this.rtmClient
|
|
|
+ .subscribe(channelName, options)
|
|
|
+ .then((res: any) => {
|
|
|
+ console.log('subscribeMessage', res);
|
|
|
+ //订阅成功
|
|
|
+ this.channelNameList[channelName] = true;
|
|
|
+ this.pageFun?.();
|
|
|
+ resolve(true);
|
|
|
+ })
|
|
|
+ .catch((err: any) => {
|
|
|
+ console.error('subscribeMessageErr', err);
|
|
|
+ reject(false);
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|
|
|
/* 取消订阅 */
|
|
|
unsubscribeMessage(channelName: string) {
|
|
@@ -234,15 +286,10 @@ export class MessageService {
|
|
|
async showMessage(param: any) {
|
|
|
let userData = await this.getUserMetadata(param.publisher);
|
|
|
let is_self = param.publisher == this.userId;
|
|
|
- // let r: any = Parse.User.current()!;
|
|
|
- // if (!is_self) {
|
|
|
- // let queryUser = new Parse.Query('_User');
|
|
|
- // queryUser.equalTo('objectId', param.publisher);
|
|
|
- // queryUser.select('avatar', 'nickname');
|
|
|
- // r = await queryUser.first();
|
|
|
- // }
|
|
|
console.log(userData);
|
|
|
let message = JSON.parse(param.message);
|
|
|
+ if (!this.messageMapList[param.channelName])
|
|
|
+ this.messageMapList[param.channelName] = [];
|
|
|
this.messageMapList[param.channelName].push({
|
|
|
is_self: is_self,
|
|
|
avatar:
|
|
@@ -250,7 +297,7 @@ export class MessageService {
|
|
|
'https://file-cloud.fmode.cn/DXNgcD6zo6/20221202/j6p8kb034039.png',
|
|
|
msg_type: 1,
|
|
|
name: userData.nickname.value ?? '未知用户',
|
|
|
- content: message?.message ?? '',
|
|
|
+ content: message?.text ?? '',
|
|
|
create_time: new Date(param.timeStamp),
|
|
|
istoday: true,
|
|
|
timeStamp: new Date(param.timeStamp),
|
|
@@ -268,10 +315,14 @@ export class MessageService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async publishMessage(message: string, channelName: string, channelType?:string) {
|
|
|
- const payload = { type: 'text', message: message };
|
|
|
+ async publishMessage(
|
|
|
+ message: string,
|
|
|
+ channelName: string,
|
|
|
+ channelType?: string
|
|
|
+ ) {
|
|
|
+ const payload = { type: 'text', text: message };
|
|
|
const publishMessage = JSON.stringify(payload);
|
|
|
- const publishOptions = { channelType: 'MESSAGE' };
|
|
|
+ const publishOptions = { channelType: channelType ?? 'MESSAGE' };
|
|
|
try {
|
|
|
const result = await this.rtmClient.publish(
|
|
|
channelName,
|