2026-09-08 15:30:29 +08:00
|
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
|
|
|
|
|
import AgoraRTC, { IAgoraRTCClient, IRemoteVideoTrack } from "agora-rtc-sdk-ng";
|
2026-09-08 15:59:41 +08:00
|
|
|
|
import { Badge, message } from "antd";
|
2026-09-14 09:12:40 +08:00
|
|
|
|
const APP_ID = "7887b4db6033410d9ac3264b40ca9c1a";
|
|
|
|
|
|
const TOKEN = "007eJxTYKjmeKazOKdUZ5qMsU389u45l6ruuuQlze8o+OZWHdBUHK3AYG5hYZ5kkpJkZmBsbGJokGKZmGxsZGaSZGKQnGiZbJh4dsuSrIZARoYFWw4zMTJAIIjPxWBiqFuSkVqUm5jDwAAA1JUhaw==";
|
2026-09-08 15:30:29 +08:00
|
|
|
|
export default function AgrtcRoomRobot({ CHANNEL }) {
|
|
|
|
|
|
const [uid] = useState(() => Number(Math.floor(Math.random() * 100000)));
|
|
|
|
|
|
const [connected, setConnected] = useState(false);
|
|
|
|
|
|
const [joined, setJoined] = useState(false);
|
2026-09-08 16:09:55 +08:00
|
|
|
|
// 标记是否存在远端视频流
|
|
|
|
|
|
const [hasRemoteVideo, setHasRemoteVideo] = useState(false);
|
2026-09-08 15:30:29 +08:00
|
|
|
|
const remoteVideoRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
const clientRef = useRef<IAgoraRTCClient | null>(null);
|
|
|
|
|
|
const remoteVideoTrackRef = useRef<IRemoteVideoTrack | null>(null);
|
|
|
|
|
|
const joiningRef = useRef(false);
|
|
|
|
|
|
const reconnectTimerRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const client = AgoraRTC.createClient({
|
|
|
|
|
|
mode: "live",
|
|
|
|
|
|
codec: "h264",
|
|
|
|
|
|
});
|
|
|
|
|
|
clientRef.current = client;
|
|
|
|
|
|
client.setClientRole("audience");
|
|
|
|
|
|
let isUnmounted = false;
|
|
|
|
|
|
|
|
|
|
|
|
// 停止并清空远端视频
|
|
|
|
|
|
const clearRemoteVideo = () => {
|
|
|
|
|
|
if (remoteVideoTrackRef.current) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
remoteVideoTrackRef.current.stop();
|
|
|
|
|
|
} catch (e) { /* */ }
|
|
|
|
|
|
remoteVideoTrackRef.current = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (remoteVideoRef.current) {
|
|
|
|
|
|
remoteVideoRef.current.innerHTML = "";
|
|
|
|
|
|
}
|
2026-09-08 16:09:55 +08:00
|
|
|
|
setHasRemoteVideo(false);
|
2026-09-08 15:30:29 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 重连逻辑
|
|
|
|
|
|
const doReconnect = async () => {
|
|
|
|
|
|
if (isUnmounted || joiningRef.current) return;
|
|
|
|
|
|
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;
|
|
|
|
|
|
if (remoteUser.hasVideo) {
|
|
|
|
|
|
await client.subscribe(remoteUser, "video");
|
|
|
|
|
|
remoteVideoTrackRef.current = remoteUser.videoTrack;
|
|
|
|
|
|
remoteUser.videoTrack?.play(remoteVideoRef.current!);
|
2026-09-08 16:09:55 +08:00
|
|
|
|
setHasRemoteVideo(true);
|
2026-09-08 15:30:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
|
if (err?.code !== "OPERATION_ABORTED") {
|
|
|
|
|
|
console.error("重连失败", err);
|
|
|
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
joiningRef.current = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 连接状态变化
|
|
|
|
|
|
const onConnStateChange = (curState: string, prevState: string) => {
|
|
|
|
|
|
if (isUnmounted) return;
|
|
|
|
|
|
console.log("连接状态变更", prevState, "→", curState);
|
|
|
|
|
|
setConnected(curState === "CONNECTED");
|
|
|
|
|
|
// 信令断开,延迟重连
|
|
|
|
|
|
if (curState === "DISCONNECTED") {
|
|
|
|
|
|
clearRemoteVideo();
|
|
|
|
|
|
if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current);
|
|
|
|
|
|
reconnectTimerRef.current = setTimeout(() => {
|
|
|
|
|
|
doReconnect();
|
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 远端用户发布流
|
|
|
|
|
|
const onUserPublished = async (user: any, mediaType: "video" | "audio") => {
|
|
|
|
|
|
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);
|
2026-09-08 16:09:55 +08:00
|
|
|
|
setHasRemoteVideo(true);
|
2026-09-08 15:30:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 远端停止发布流(上位机停止推流)
|
|
|
|
|
|
const onUserUnpublished = (user: any, mediaType: "video" | "audio") => {
|
|
|
|
|
|
if (isUnmounted) return;
|
|
|
|
|
|
console.log("user-unpublished", user.uid, mediaType);
|
|
|
|
|
|
if (mediaType === "video") {
|
|
|
|
|
|
clearRemoteVideo();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 远端用户离开频道
|
|
|
|
|
|
const onUserLeft = (user: any) => {
|
|
|
|
|
|
if (isUnmounted) return;
|
|
|
|
|
|
console.log("user-left", user.uid);
|
|
|
|
|
|
clearRemoteVideo();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 远端用户进入
|
|
|
|
|
|
const onUserJoined = (user: any) => {
|
|
|
|
|
|
if (!isUnmounted) console.log("远端用户进入频道:", user.uid);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Token即将过期提醒(测试Token有效期有限)
|
|
|
|
|
|
const onTokenWillExpire = () => {
|
|
|
|
|
|
console.warn("token即将过期,请更新token");
|
|
|
|
|
|
message.warning("Token即将过期,视频可能很快断开");
|
|
|
|
|
|
// 正式环境:请求后端拿新token client.renewToken(newToken)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 注册事件,保存回调引用用于off解绑
|
|
|
|
|
|
client.on("connection-state-change", onConnStateChange);
|
|
|
|
|
|
client.on("user-published", onUserPublished);
|
|
|
|
|
|
client.on("user-unpublished", onUserUnpublished);
|
|
|
|
|
|
client.on("user-left", onUserLeft);
|
|
|
|
|
|
client.on("user-joined", onUserJoined);
|
|
|
|
|
|
client.on("token-privilege-will-expire", onTokenWillExpire);
|
|
|
|
|
|
|
|
|
|
|
|
const init = async () => {
|
|
|
|
|
|
if (joiningRef.current) return;
|
|
|
|
|
|
joiningRef.current = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
await client.join(APP_ID, CHANNEL, TOKEN, uid);
|
|
|
|
|
|
if (isUnmounted) {
|
|
|
|
|
|
console.log("组件已卸载,丢弃join结果");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log("加入频道成功");
|
|
|
|
|
|
setJoined(true);
|
|
|
|
|
|
message.success("RTC已加入频道");
|
|
|
|
|
|
// 上位机已经提前推流,手动订阅
|
|
|
|
|
|
for (const remoteUser of client.remoteUsers) {
|
|
|
|
|
|
if (isUnmounted) break;
|
|
|
|
|
|
console.log("发现已有远端用户:", remoteUser.uid);
|
|
|
|
|
|
if (remoteUser.hasVideo) {
|
|
|
|
|
|
await client.subscribe(remoteUser, "video");
|
|
|
|
|
|
remoteVideoTrackRef.current = remoteUser.videoTrack;
|
|
|
|
|
|
remoteUser.videoTrack?.play(remoteVideoRef.current!);
|
2026-09-08 16:09:55 +08:00
|
|
|
|
setHasRemoteVideo(true);
|
2026-09-08 15:30:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
|
if (err?.code === "OPERATION_ABORTED") {
|
|
|
|
|
|
console.log("join操作被取消(StrictMode/组件卸载),忽略");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
console.error("加入频道失败:", err);
|
|
|
|
|
|
if (!isUnmounted) {
|
|
|
|
|
|
message.error("加入频道失败,请检查appid/token/channel");
|
|
|
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
joiningRef.current = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
|
|
|
|
// 组件卸载清理
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
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;
|
|
|
|
|
|
};
|
2026-09-08 15:59:41 +08:00
|
|
|
|
}, [uid, CHANNEL]);
|
2026-09-08 15:30:29 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
2026-09-08 15:59:41 +08:00
|
|
|
|
<div style={{ width: "100%", height: "100%", display: "flex", flexDirection: "column", borderRadius: 8, overflow: "hidden", background: "#000" }}>
|
|
|
|
|
|
<div style={{ display: "flex", justifyContent: "end", alignItems: "center", gap: 12, padding: "4px 8px", background: "rgba(255,255,255,0.08)", flexShrink: 0 }}>
|
|
|
|
|
|
<Badge
|
|
|
|
|
|
color={connected ? "green" : "red"}
|
|
|
|
|
|
text={connected ? "RTC已连接" : "RTC未连接"}
|
|
|
|
|
|
styles={{ content: { color: '#888' } }}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<Badge
|
|
|
|
|
|
color={joined ? "green" : "orange"}
|
|
|
|
|
|
text={joined ? "已加入频道" : "未加入频道"}
|
|
|
|
|
|
styles={{ content: { color: '#888' } }}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span style={{ fontSize: 11, color: "#888" }}>频道:{CHANNEL}</span>
|
|
|
|
|
|
</div>
|
2026-09-08 16:09:55 +08:00
|
|
|
|
<div style={{ flex: 1, width: "100%", minHeight: 0, position: "relative" }}>
|
|
|
|
|
|
{/* 无视频时显示加载占位 */}
|
|
|
|
|
|
{!hasRemoteVideo && (
|
|
|
|
|
|
<div style={{
|
|
|
|
|
|
position: "absolute",
|
|
|
|
|
|
inset: 0,
|
|
|
|
|
|
display: "flex",
|
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
|
color: "#aaa",
|
|
|
|
|
|
fontSize: 14,
|
|
|
|
|
|
background: "#000"
|
|
|
|
|
|
}}>
|
|
|
|
|
|
视频加载中,等待远端推流...
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{/* Agora 视频容器,内部会被SDK填充video dom */}
|
|
|
|
|
|
<div ref={remoteVideoRef} style={{
|
|
|
|
|
|
position: "absolute",
|
|
|
|
|
|
inset: 0
|
|
|
|
|
|
}} />
|
|
|
|
|
|
</div>
|
2026-09-08 15:59:41 +08:00
|
|
|
|
</div >
|
2026-09-08 15:30:29 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|