From 7ebba1bbf6313f9e9cc3c4f88b52b38e24b17e87 Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Tue, 8 Sep 2026 16:09:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A7=86=E9=A2=91=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=8A=A0=E8=BD=BD=E5=8D=A0=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/AgrtcRoomRobot.tsx | 43 ++++++++++++++------- src/components/devices/DeviceStatusPage.tsx | 10 ++--- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/components/AgrtcRoomRobot.tsx b/src/components/AgrtcRoomRobot.tsx index a473d79..adee0fb 100644 --- a/src/components/AgrtcRoomRobot.tsx +++ b/src/components/AgrtcRoomRobot.tsx @@ -1,19 +1,17 @@ import React, { useEffect, useRef, useState } from "react"; import AgoraRTC, { IAgoraRTCClient, IRemoteVideoTrack } from "agora-rtc-sdk-ng"; import { Badge, message } from "antd"; - const APP_ID = "875f6494d87548e783fefbf33f598c97"; const TOKEN = "007eJxTYIjPuZr09PC0znd6+z0MMyL6ZHb68OetXW+kO1vHU2ZNtYECg4W5aZqZiaVJCpBhYpFqbmGclpqWlGZsnGZqaZFsaT6pZH5WQyAjA7e+LysjAwSC+JwMxmYlqcUlugZGDAwAbuYeJg=="; - export default function AgrtcRoomRobot({ CHANNEL }) { const [uid] = useState(() => Number(Math.floor(Math.random() * 100000))); const [connected, setConnected] = useState(false); const [joined, setJoined] = useState(false); - + // 标记是否存在远端视频流 + const [hasRemoteVideo, setHasRemoteVideo] = useState(false); const remoteVideoRef = useRef(null); const clientRef = useRef(null); const remoteVideoTrackRef = useRef(null); - const joiningRef = useRef(false); const reconnectTimerRef = useRef(null); @@ -24,7 +22,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) { }); clientRef.current = client; client.setClientRole("audience"); - let isUnmounted = false; // 停止并清空远端视频 @@ -38,6 +35,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) { if (remoteVideoRef.current) { remoteVideoRef.current.innerHTML = ""; } + setHasRemoteVideo(false); }; // 重连逻辑 @@ -46,14 +44,12 @@ export default function AgrtcRoomRobot({ CHANNEL }) { console.log("执行RTC重连"); clearRemoteVideo(); setJoined(false); - try { joiningRef.current = true; await client.join(APP_ID, CHANNEL, TOKEN, uid); if (isUnmounted) return; setJoined(true); message.info("RTC重连成功"); - // 重连成功后重新订阅已经在线的上位机 for (const remoteUser of client.remoteUsers) { if (isUnmounted) break; @@ -61,6 +57,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) { await client.subscribe(remoteUser, "video"); remoteVideoTrackRef.current = remoteUser.videoTrack; remoteUser.videoTrack?.play(remoteVideoRef.current!); + setHasRemoteVideo(true); } } } catch (err: any) { @@ -77,7 +74,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) { if (isUnmounted) return; console.log("连接状态变更", prevState, "→", curState); setConnected(curState === "CONNECTED"); - // 信令断开,延迟重连 if (curState === "DISCONNECTED") { clearRemoteVideo(); @@ -93,11 +89,11 @@ export default function AgrtcRoomRobot({ CHANNEL }) { if (isUnmounted) return; console.log("user-published", user.uid, mediaType); await client.subscribe(user, mediaType); - if (mediaType === "video" && remoteVideoRef.current) { clearRemoteVideo(); remoteVideoTrackRef.current = user.videoTrack; user.videoTrack?.play(remoteVideoRef.current); + setHasRemoteVideo(true); } }; @@ -149,7 +145,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) { console.log("加入频道成功"); setJoined(true); message.success("RTC已加入频道"); - // 上位机已经提前推流,手动订阅 for (const remoteUser of client.remoteUsers) { if (isUnmounted) break; @@ -158,6 +153,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) { await client.subscribe(remoteUser, "video"); remoteVideoTrackRef.current = remoteUser.videoTrack; remoteUser.videoTrack?.play(remoteVideoRef.current!); + setHasRemoteVideo(true); } } } catch (err: any) { @@ -181,17 +177,14 @@ export default function AgrtcRoomRobot({ CHANNEL }) { isUnmounted = true; if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current); clearRemoteVideo(); - const c = clientRef.current; if (!c) return; - c.off("connection-state-change", onConnStateChange); c.off("user-published", onUserPublished); c.off("user-unpublished", onUserUnpublished); c.off("user-left", onUserLeft); c.off("user-joined", onUserJoined); c.off("token-privilege-will-expire", onTokenWillExpire); - c.leave(); clientRef.current = null; }; @@ -210,10 +203,30 @@ export default function AgrtcRoomRobot({ CHANNEL }) { text={joined ? "已加入频道" : "未加入频道"} styles={{ content: { color: '#888' } }} /> - 频道:{CHANNEL} -
+
+ {/* 无视频时显示加载占位 */} + {!hasRemoteVideo && ( +
+ 视频加载中,等待远端推流... +
+ )} + {/* Agora 视频容器,内部会被SDK填充video dom */} +
+
); } diff --git a/src/components/devices/DeviceStatusPage.tsx b/src/components/devices/DeviceStatusPage.tsx index ad06527..db173e6 100644 --- a/src/components/devices/DeviceStatusPage.tsx +++ b/src/components/devices/DeviceStatusPage.tsx @@ -1224,7 +1224,7 @@ export default function DeviceStatusPage() { {/* 视频层:停止时不渲染 / 未播放时为空,不影响状态面板 */} {isVideoPlaying && ( mowerView === 'all' ? ( - robotVideo ? ( + (
- ) : null + ) ) : mowerView === 'front' ? (
{device?.hasHost ? ( - ) : robotVideo ? ( + ) : ( - ) : null} + )}
) : (
@@ -1268,7 +1268,7 @@ export default function DeviceStatusPage() { )} {/* 视频未加载 / 停止播放的占位 */} - {(!robotVideo && !isVideoPlaying) && ( + {!isVideoPlaying && (