Bläddra i källkod

update 直播服务

warrior 4 månader sedan
förälder
incheckning
4684de83b0
1 ändrade filer med 104 tillägg och 15 borttagningar
  1. 104 15
      projects/live-app/src/services/live.service.ts

+ 104 - 15
projects/live-app/src/services/live.service.ts

@@ -51,7 +51,7 @@ export class LiveService {
   room?: Parse.Object; //直播间
   connection_state?: string; //连接状态
   surplusNumber: number = 0; //剩余可通话时长(单位:秒s)
-
+  liveLog?: Parse.Object; //直播记录
   constructor(
     private http: HttpService,
     private aiServ: AiChatService,
@@ -184,11 +184,14 @@ export class LiveService {
       .then(async (uid: any) => {
         // await this.client.setClientRole('host');
         console.log('进入频道当前uid:', uid);
-        this.client.connectionState;
         this.connection_state = this.client.connectionState;
+        if (this.room?.get('user')?.id !== Parse.User.current()?.id) {
+          // 观众进入直播间,创建直播记录
+          await this.createLiveLog(uid);
+        }
         await this.publishSelf();
         await this.joinReady();
-        this.monitorDevices();
+        this.afterJoin();
       })
       .catch((err: any) => {
         console.log('进入频道失败:', err);
@@ -264,6 +267,10 @@ export class LiveService {
         if (remoteEle) {
           remoteEle.textContent = '对方离开直播间';
         }
+        //主播离开,停止计时计费
+        if(this.room?.get('user').id !== Parse.User.current()?.id){
+          this.client.leave()
+        }
         this.alertTips('对方已离开直播间');
       }
     });
@@ -271,28 +278,110 @@ export class LiveService {
       'connection-state-change',
       (curState: any, prevState: any) => {
         this.connection_state = curState;
+        if(curState == 'RECONNECTING' || curState == 'DISCONNECTED' || curState == 'DISCONNECTING'){
+          this.timer && clearInterval(this.timer);
+        }
         console.log(this.connection_state);
         console.log(prevState);
       }
     );
+    this.monitorDevices();
+  }
+  async createLiveLog(uid: string) {
+    let profile = JSON.parse(localStorage.getItem('profile') || '{}');
+    let obj = Parse.Object.extend('LiveLog');
+    let liveLog = new obj();
+    liveLog.set('title', `与${profile?.name || profile?.mobile}进行直播`);
+    liveLog.set('uid', String(uid));
+    liveLog.set('company', {
+      __type: 'Pointer',
+      className: 'Company',
+      objectId: this.company,
+    });
+    liveLog.set('room', {
+      __type: 'Pointer',
+      className: 'Room',
+      objectId: this.room?.id,
+    });
+    liveLog.set('user', {
+      __type: 'Pointer',
+      className: '_User',
+      objectId: Parse.User.current()?.id,
+    });
+    liveLog.set('profile', {
+      __type: 'Pointer',
+      className: 'Profile',
+      objectId: this.room?.get('profile')?.id,
+    });
+    this.liveLog = await liveLog.save();
+  }
+  /* 连线成功立即创建直播记录,同步获取剩余可通话时长,并且开始计时 */
+  async afterJoin() {
+    this.timer && clearTimeout(this.timer);
+    if (this.client.remoteUsers.length > 0) {
+      if (this.room?.get('user')?.id === Parse.User.current()?.id) {
+        //如果是主播,进入获取remoteUsers.user获取livelog再获取对方剩余通话时长
+        this.getLiveLog()
+      } else {
+        //首次进入至少计时2分钟,不足2分钟按2分钟计算扣时
+        await this.get_duration();
+        this.computeDuration(60000 * 2);
+      }
+    } else {
+      this.timer = setTimeout(() => {
+        this.afterJoin();
+      }, 1000);
+    }
+  }
+  async getLiveLog(){
+    let uid = this.client.remoteUsers[0].uid
+    this.timer && clearInterval(this.timer);
+    let query = new Parse.Query('LiveLog')
+    query.equalTo('uid',String(uid))
+    query.equalTo('room',this.room?.id)
+    query.notEqualTo('isDeleted',true)
+    query.notEqualTo('isLive',true)
+    query.descending('createdAt')
+    this.liveLog = await query.first()
+    if(this.liveLog?.id){
+      this.liveLog?.set('isLive',true)
+      await this.liveLog?.save()
+      this.get_duration()
+      return
+    }
+    this.timer = setTimeout(() => {
+      this.getLiveLog();
+    }, 1000);
+  }
+  async get_duration() {
+    let url = 'http://localhost:7337/api/ailiao/remain_second';
+    let params = {
+      rid: this.room?.id,
+      uid: this.liveLog?.get('user')?.id || this.liveLog?.get('user')?.objectId,
+    };
+    let data = await this.http.httpRequst(url, params, 'POST');
+    console.log(data);
+    this.surplusNumber = data.data.secoud ?? 0;
   }
 
   /* 开始计时 */
-  computeDuration() {
+  async computeDuration(num?: number) {
     //每10秒保存一次数据
     this.timer && clearInterval(this.timer);
-    this.timer = setInterval(() => {
-      let url = 'http://test.fmode.cn/api/ailiao/count/duration';
-      let params = {
-        company: this.company,
-        profile:this.profile?.id,
-        rid: this.room?.id,
-        uid: Parse.User.current()?.id,
-      };
-      this.http.httpRequst(url, params,'POST');
-    }, 10000);
+    let url = 'http://localhost:7337/api/ailiao/count/duration';
+    let params = {
+      company: this.company,
+      profile: this.profile?.id,
+      rid: this.room?.id,
+      uid: Parse.User.current()?.id,
+      duration: num ? num/1000 : 10,
+    };
+    let data = await this.http.httpRequst(url, params, 'POST');
+    console.log(data);
+    this.timer = setTimeout(() => {
+      this.computeDuration();
+    }, num ?? 10000);
   }
-
   /* 监听音视频设备插拔 */
   monitorDevices() {
     AgoraRTC.onMicrophoneChanged = async (changedDevice: any) => {