Преглед изворни кода

优化RTC频道管理:增加发布选项参数,完善加入/离开逻辑,移除冗余监听

yourname пре 7 месеци
родитељ
комит
860b90a6e6
1 измењених фајлова са 51 додато и 8 уклоњено
  1. 51 8
      client/mobile/pages_classroom.tsx

+ 51 - 8
client/mobile/pages_classroom.tsx

@@ -472,10 +472,11 @@ export const ClassroomPage = () => {
       await gm!.joinGroup(classId);
       listenGroupEvents();
       listenMessageEvents();
-      listenRtcEvents();
 
       // 加入RTC频道
-      await joinRtcChannel();
+      await joinRtcChannel(classId, {
+        publishVideo:false
+      });
 
       setIsJoinedClass(true);
       setErrorMessage('');
@@ -537,7 +538,7 @@ export const ClassroomPage = () => {
     
     try {
 
-      // 发送开始上课消息
+            // 发送开始上课消息
       await imMessageManager.current.sendGroupMessage({
         groupId: classId,
         data: JSON.stringify({ action: 'start_class' }),
@@ -564,6 +565,14 @@ export const ClassroomPage = () => {
       });
       setClassStatus(ClassStatus.ENDED);
       showToast('success', '课堂已结束');
+      
+      // 离开RTC频道
+      try {
+        await leaveRtcChannel();
+      } catch (err: any) {
+        console.error('离开RTC频道失败:', err);
+        showToast('error', '离开RTC频道失败');
+      }
     } catch (err: any) {
       setErrorMessage(`结束上课失败: ${err.message}`);
     }
@@ -640,6 +649,18 @@ export const ClassroomPage = () => {
           listenMessageEvents();
         }
         
+        // 加入RTC频道
+        await joinRtcChannel(response.groupId);
+        // try {
+        //   await joinRtcChannel(response.groupId);
+        // } catch (err: any) {
+        //   showToast('error', `加入RTC频道失败: ${err.message}`);
+        //   // 回滚IM状态
+        //   await groupManager.leaveGroup(response.groupId);
+        //   setIsJoinedClass(false);
+        //   throw new Error(`创建成功但加入RTC失败: ${err.message}`);
+        // }
+        
         // 记录创建时间
         const createTime = new Date();
         showMessage(`创建时间: ${createTime.toLocaleString()}`);
@@ -663,11 +684,23 @@ export const ClassroomPage = () => {
   };
 
   // 加入RTC频道
-  const joinRtcChannel = async () => {
+  const joinRtcChannel = async (classId: string, publishOptions?: {
+    publishVideo?: boolean
+    publishAudio?: boolean
+    publishScreen?: boolean
+  }) => {
     if (!aliRtcEngine.current) return;
+    const {
+      publishVideo = false,
+      publishAudio = false,
+      publishScreen = false,
+    } = publishOptions || {};
     
     const timestamp = Math.floor(Date.now() / 1000) + 3600 * 3;
     const token = await generateToken(RTC_APP_ID, RTC_APP_KEY, classId, userId, timestamp);
+    await aliRtcEngine.current.publishLocalVideoStream(publishVideo);
+    await aliRtcEngine.current.publishLocalAudioStream(publishAudio);
+    await aliRtcEngine.current.publishLocalScreenShareStream(publishScreen);
     await aliRtcEngine.current.joinChannel(
       {
         channelId: classId,
@@ -678,29 +711,39 @@ export const ClassroomPage = () => {
       },
       userId
     );
-    showToast('info', '已加入RTC频道');
+    // showToast('info', '已加入RTC频道');
   };
 
   // 离开RTC频道
   const leaveRtcChannel = async () => {
     if (!aliRtcEngine.current) return;
     await aliRtcEngine.current.leaveChannel();
-    showToast('info', '已离开RTC频道');
+    // showToast('info', '已离开RTC频道');
   };
 
   // 开启摄像头预览
   const startCameraPreview = async () => {
     if (!aliRtcEngine.current) return;
+    
+    await leaveRtcChannel()
+    await joinRtcChannel(classId, {
+      publishVideo:true
+    })
     aliRtcEngine.current.setLocalViewConfig('localPreviewer', AliRtcVideoTrack.AliRtcVideoTrackCamera);
     await aliRtcEngine.current.startPreview();
-    showToast('info', '摄像头已开启');
+    // showToast('info', '摄像头已开启');
   };
 
   // 关闭摄像头预览
   const stopCameraPreview = async () => {
     if (!aliRtcEngine.current) return;
+
+    await leaveRtcChannel()
+    await joinRtcChannel(classId, {
+      publishVideo: false
+    })
     await aliRtcEngine.current.stopPreview();
-    showToast('info', '摄像头已关闭');
+    // showToast('info', '摄像头已关闭');
   };
 
   // 切换摄像头状态