|
@@ -1,14 +1,18 @@
|
|
|
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';
|
|
|
+import { Subject } from 'rxjs';
|
|
|
+import { Router } from '@angular/router';
|
|
|
declare const AgoraRTM: any;
|
|
|
@Injectable({
|
|
|
providedIn: 'root',
|
|
|
})
|
|
|
export class MessageService {
|
|
|
+ private eventSource = new Subject<any>();
|
|
|
+ event$ = this.eventSource.asObservable();
|
|
|
+ company: string = 'Qje9D4bqol';
|
|
|
rtmClient: any; // RTM实例
|
|
|
// rtmClientMap: any = {};
|
|
|
channelNameList: any = {}; //订阅频道状态
|
|
@@ -21,9 +25,10 @@ export class MessageService {
|
|
|
|
|
|
pageFun?: Function; //页面传入的方法
|
|
|
alert: any; // 弹窗
|
|
|
+
|
|
|
constructor(
|
|
|
- private liveService: LiveService,
|
|
|
private alertController: AlertController,
|
|
|
+ private router: Router,
|
|
|
public toastController: ToastController,
|
|
|
private http: HttpService
|
|
|
) {}
|
|
@@ -90,7 +95,7 @@ export class MessageService {
|
|
|
let uid = Parse.User.current()?.id;
|
|
|
let baseurl = 'https://server.fmode.cn/api/webrtc/build_token';
|
|
|
let reqBody = {
|
|
|
- company: this.liveService.company, // this.aiSer.company,
|
|
|
+ company: this.company,
|
|
|
channelName: this.msChannelName,
|
|
|
type: 'withrtm',
|
|
|
account: uid,
|
|
@@ -119,69 +124,133 @@ export class MessageService {
|
|
|
}
|
|
|
/**@监听频道消息
|
|
|
*@'USERCALLINVITATION': 用户通话邀请
|
|
|
- *@'REFUSE': 拒绝
|
|
|
- *@'CLOASE': 拒绝
|
|
|
+ *@'CLOASEINVITATION': 取消通话邀请
|
|
|
+ *@'REFUSEINVITATION_' + uid: 拒绝通话邀请
|
|
|
+ *@'RESPONSEINVITOIN_' + uid: 接受邀请
|
|
|
*/
|
|
|
joinReady(channelName?: string) {
|
|
|
this.rtmClient.addEventListener('message', async (event: any) => {
|
|
|
console.log('接收到一条消息:', event);
|
|
|
+ let states = [
|
|
|
+ 'USERCALLINVITATION',
|
|
|
+ 'REFUSEINVITATION_' + this.userId,
|
|
|
+ 'CLOASEINVITATION',
|
|
|
+ 'RESPONSEINVITOIN_' + this.userId,
|
|
|
+ ];
|
|
|
const message = JSON.parse(event.message);
|
|
|
let is_self = event.publisher == this.userId;
|
|
|
- let userData = await this.getUserMetadata(event.publisher);
|
|
|
+ console.log('自己发出的消息:', is_self, message.text);
|
|
|
+ if (!is_self && states.includes(message.text)) {
|
|
|
+ this.callPresence(message.text, event.publisher, event.channelName);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.showMessage(event);
|
|
|
+ });
|
|
|
+ this.rtmClient.addEventListener('presence', (event: any) => {
|
|
|
+ console.log('频道人员状态变化 ', event);
|
|
|
+ let is_self = event.publisher == this.userId;
|
|
|
+ //远端用户离开频道,主播在线状态
|
|
|
if (
|
|
|
- event.channelName == this.userId &&
|
|
|
- message.text == 'USERCALLINVITATION' &&
|
|
|
- !is_self
|
|
|
+ !is_self &&
|
|
|
+ event.eventType === 'REMOTE_LEAVE' &&
|
|
|
+ event.channelName === this.userId
|
|
|
) {
|
|
|
- if (this.alert) return;
|
|
|
+ this.setConnectState(this.userId, 'ONLINE');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this.rtmClient.addEventListener('linkState', (event: any) => {
|
|
|
+ console.log('连接状态: ', event);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 设置频道状态 */
|
|
|
+ async setConnectState(channelName: string, mode: string) {
|
|
|
+ const channelType = 'MESSAGE';
|
|
|
+ const states = {
|
|
|
+ Mode: mode,
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ await this.rtmClient.presence.setState(channelName, channelType, states);
|
|
|
+ } catch (err: any) {
|
|
|
+ console.log(err);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 呼叫事件 */
|
|
|
+ async callPresence(message: string, publisher: string, channelName: string) {
|
|
|
+ console.log(message);
|
|
|
+ let userData = await this.getUserMetadata(publisher);
|
|
|
+ let toast;
|
|
|
+ switch (message) {
|
|
|
+ case 'USERCALLINVITATION':
|
|
|
+ await this.setConnectState(this.userId, 'CONNECTING');
|
|
|
console.log(`收到${userData.nickname.value ?? '未知用户'}通话邀请`);
|
|
|
this.alert = await this.alertController.create({
|
|
|
cssClass: 'my-custom-class',
|
|
|
header: '通话邀请',
|
|
|
message: `收到${userData.nickname.value ?? '未知用户'}通话邀请`,
|
|
|
+ backdropDismiss: false,
|
|
|
buttons: [
|
|
|
{
|
|
|
text: '拒绝',
|
|
|
role: 'cancel',
|
|
|
- handler: (blah) => {
|
|
|
- this.publishMessage('REFUSE', event.channelName);
|
|
|
+ handler: async (blah) => {
|
|
|
+ await this.setConnectState(this.userId, 'ONLINE');
|
|
|
+ this.publishMessage(
|
|
|
+ 'REFUSEINVITATION_' + publisher,
|
|
|
+ channelName
|
|
|
+ );
|
|
|
},
|
|
|
},
|
|
|
{
|
|
|
text: '接受',
|
|
|
cssClass: 'secondary',
|
|
|
- handler: () => {
|
|
|
- // this.sendVideoCallInvite();
|
|
|
+ handler: async () => {
|
|
|
+ this.publishMessage(
|
|
|
+ 'RESPONSEINVITOIN_' + publisher,
|
|
|
+ channelName
|
|
|
+ );
|
|
|
+ let rid = await this.getRoom(this.userId);
|
|
|
+ this.router.navigate(['live/link-room/' + rid]);
|
|
|
},
|
|
|
},
|
|
|
],
|
|
|
});
|
|
|
await this.alert.present();
|
|
|
- return;
|
|
|
- } else if (
|
|
|
- event.channelName == this.userId &&
|
|
|
- message.text == 'CLOASE' &&
|
|
|
- !is_self
|
|
|
- ) {
|
|
|
+ break;
|
|
|
+ case 'CLOASEINVITATION':
|
|
|
+ await this.setConnectState(this.userId, 'ONLINE');
|
|
|
console.log(`${userData.nickname.value ?? '未知用户'}取消通话`);
|
|
|
this.alert?.dismiss();
|
|
|
- const toast = await this.toastController.create({
|
|
|
- message: '对方已取消视频通话邀请',
|
|
|
+ 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.rtmClient.addEventListener('linkState', (event: any) => {
|
|
|
- console.log('连接状态: ', event);
|
|
|
- // this.rtmClientMap[channelName].connectState = event.currentState;
|
|
|
- });
|
|
|
+ break;
|
|
|
+ case 'REFUSEINVITATION_' + this.userId:
|
|
|
+ console.log(`${userData.nickname.value ?? '未知用户'}拒绝通话`);
|
|
|
+ this.alert?.dismiss();
|
|
|
+ this.eventSource.next(false);
|
|
|
+ break;
|
|
|
+ case 'RESPONSEINVITOIN_' + this.userId:
|
|
|
+ console.log(
|
|
|
+ `${userData.nickname.value ?? '未知用户'}同意通话,进入视频通话`
|
|
|
+ );
|
|
|
+ this.alert?.dismiss();
|
|
|
+ this.eventSource.next(true);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async getRoom(uid: string): Promise<string | undefined> {
|
|
|
+ let query = new Parse.Query('Room');
|
|
|
+ query.equalTo('company', this.company);
|
|
|
+ query.equalTo('user', uid);
|
|
|
+ query.notEqualTo('isDeleted', true);
|
|
|
+ query.select('objectId');
|
|
|
+ let r = await query.first();
|
|
|
+ return r?.id;
|
|
|
}
|
|
|
/* 加入频道 */
|
|
|
// async join(channelName: string) {
|