Files
web-Iot/src/components/AgoraRtc.tsx

134 lines
3.0 KiB
TypeScript
Raw Normal View History

2026-05-12 11:21:31 +08:00
import React, {
useEffect,
useRef,
useState,
} from "react";
import AgoraRTC from "agora-rtc-sdk-ng";
2026-06-03 16:51:49 +08:00
import { message } from "antd";
2026-05-12 11:11:30 +08:00
2026-06-03 16:51:49 +08:00
// 工具:解析URL参数
const parseUrlParams = (url) => {
2026-06-03 16:51:49 +08:00
if (!url) return {};
const params = new URLSearchParams(url);
return {
appId: params.get("app_id") || "",
channel: params.get("channel") || "",
token: params.get("token") || "",
uid: params.get("uid") || String(Math.floor(Math.random() * 100000)),
};
};
2026-06-03 16:51:49 +08:00
export default function AgoraRtcRoom({ url = "" }) {
const { appId, channel, token, uid } = parseUrlParams(url);
2026-06-03 16:51:49 +08:00
// 关键修复:client 改成组件内 ref,不再全局!
const clientRef = useRef(null);
const [connected, setConnected] = useState(false);
const [joined, setJoined] = useState(false);
// 只显示远端画面(设备画面)
const remoteVideoRef = useRef(null);
// 真正的销毁函数
const destroy = async () => {
const client = clientRef.current;
if (client) {
client.removeAllListeners();
await client.leave().catch(() => { });
clientRef.current = null;
}
setJoined(false);
setConnected(false);
if (remoteVideoRef.current) {
remoteVideoRef.current.innerHTML = "";
}
};
2026-05-12 11:11:30 +08:00
useEffect(() => {
2026-06-03 16:51:49 +08:00
// 必须参数检查
if (!appId || !channel || !token || !uid) {
console.log("RTC参数不全,无法播放");
return;
}
2026-05-12 11:11:30 +08:00
2026-06-03 16:51:49 +08:00
// 每次都创建新实例,彻底杜绝复用导致黑屏
const client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
clientRef.current = client;
2026-05-12 12:36:07 +08:00
2026-06-03 16:51:49 +08:00
const init = async () => {
await destroy(); // 先清理上一次
// 状态监听
client.on("connection-state-change", (state) => {
setConnected(state === "CONNECTED");
});
// 远端视频来了 → 自动播放
client.on("user-published", async (user, mediaType) => {
try {
await client.subscribe(user, mediaType);
if (mediaType === "video" && remoteVideoRef.current) {
user.videoTrack.play(remoteVideoRef.current);
2026-05-12 11:11:30 +08:00
}
2026-06-03 16:51:49 +08:00
if (mediaType === "audio") {
user.audioTrack.play();
2026-05-12 11:11:30 +08:00
}
2026-06-03 16:51:49 +08:00
} catch (err) {
console.error("订阅失败", err);
}
});
2026-05-12 11:11:30 +08:00
2026-06-03 16:51:49 +08:00
// 远端离开 → 清空画面
client.on("user-left", () => {
if (remoteVideoRef.current) {
remoteVideoRef.current.innerHTML = "";
}
});
2026-05-12 12:36:07 +08:00
2026-06-03 16:51:49 +08:00
try {
await client.join(appId, channel, token, uid);
2026-05-12 12:36:07 +08:00
setJoined(true);
2026-06-03 16:51:49 +08:00
console.log("加入频道成功");
2026-05-12 11:11:30 +08:00
} catch (err) {
2026-06-03 16:51:49 +08:00
console.error("加入失败", err);
message.error("视频连接失败");
2026-05-12 11:11:30 +08:00
}
};
init();
2026-06-03 16:51:49 +08:00
// 组件销毁/URL变化 → 彻底清理
2026-05-12 11:11:30 +08:00
return () => {
2026-06-03 16:51:49 +08:00
destroy();
2026-05-12 11:11:30 +08:00
};
2026-06-03 16:51:49 +08:00
// 关键:URL变化必须重连
}, [appId, channel, token, uid]);
2026-05-12 11:11:30 +08:00
2026-06-03 16:51:49 +08:00
// 你只看设备视频 → 不需要推流,我直接删掉了
2026-05-12 11:11:30 +08:00
return (
<div
2026-05-12 11:11:30 +08:00
style={{
display: "flex",
gap: 20,
width: "100%",
height: '100%'
2026-05-12 11:11:30 +08:00
}}
>
<div
2026-06-03 16:51:49 +08:00
ref={remoteVideoRef}
2026-05-12 11:11:30 +08:00
style={{
2026-05-12 11:21:31 +08:00
width: "100%",
height: '100%',
2026-06-03 16:51:49 +08:00
background: "#000",
borderRadius: 8,
2026-06-03 16:51:49 +08:00
overflow: "hidden",
2026-05-12 11:11:30 +08:00
}}
/>
</div>
2026-05-12 11:11:30 +08:00
);
}