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

306 lines
4.1 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-05-12 11:11:30 +08:00
import {
Button,
Card,
Badge,
message,
2026-05-12 11:21:31 +08:00
Space,
2026-05-12 11:11:30 +08:00
} from "antd";
const APP_ID =
"62a87b833b7e48888aca06b625f88c14";
const CHANNEL =
"test_rtc_channel";
2026-05-12 11:21:31 +08:00
const TOKEN = "007eJxTYGhZyLP1Ekt5av/jV1mPpi98fG75FqcXvDl7/wuUJjNeEVukwGBmlGhhnmRhbJxknmpiAQSJyYkGZklmRqZpFhbJhiYfZjJlNQQyMrDxcrIwMkAgiC/AUJJaXBJfVJIcn5yRmJeXmsPAAABcCyR4";
2026-05-12 11:11:30 +08:00
const client = AgoraRTC.createClient({
mode: "rtc",
codec: "vp8",
});
const parseUrlParams = (url) => {
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)),
sn: params.get("sn") || "",
};
};
export default function AgoraRtcRoom({ url = "" }) {
const { appId, channel, token, uid } = parseUrlParams(url);
//const [uid] = useState(() =>
// String(
// Math.floor(
// Math.random() * 100000
// )
// )
//);
2026-05-12 11:11:30 +08:00
const [connected, setConnected] =
useState(false);
const [published, setPublished] =
useState(false);
2026-05-12 12:36:07 +08:00
const [joined, setJoined] =
useState(false);
2026-05-12 11:21:31 +08:00
const localVideoRef =
useRef<HTMLDivElement>(null);
const remoteVideoRef =
useRef<HTMLDivElement>(null);
2026-05-12 11:11:30 +08:00
useEffect(() => {
const init = async () => {
try {
2026-05-12 12:36:07 +08:00
// RTC 状态监听
2026-05-12 11:11:30 +08:00
client.on(
"connection-state-change",
(state) => {
console.log(
2026-05-12 12:36:07 +08:00
"RTC状态:",
2026-05-12 11:11:30 +08:00
state
);
if (
2026-05-12 11:21:31 +08:00
state ===
"CONNECTED"
2026-05-12 11:11:30 +08:00
) {
2026-05-12 11:21:31 +08:00
setConnected(
true
2026-05-12 11:11:30 +08:00
);
2026-05-12 12:36:07 +08:00
} else {
setConnected(
false
);
2026-05-12 11:11:30 +08:00
}
}
);
2026-05-12 12:36:07 +08:00
// 远端用户发布流
2026-05-12 11:11:30 +08:00
client.on(
"user-published",
2026-05-12 11:21:31 +08:00
async (
user,
mediaType
) => {
2026-05-12 11:11:30 +08:00
console.log(
2026-05-12 12:36:07 +08:00
"远端用户发布:",
2026-05-12 11:11:30 +08:00
user.uid,
mediaType
);
2026-05-12 12:36:07 +08:00
// 订阅远端流
2026-05-12 11:11:30 +08:00
await client.subscribe(
user,
mediaType
);
2026-05-12 11:21:31 +08:00
// 视频
2026-05-12 11:11:30 +08:00
if (
2026-05-12 11:21:31 +08:00
mediaType ===
"video"
2026-05-12 11:11:30 +08:00
) {
2026-05-12 11:21:31 +08:00
user.videoTrack?.play(
remoteVideoRef.current!
2026-05-12 11:11:30 +08:00
);
}
2026-05-12 11:21:31 +08:00
// 音频
if (
mediaType ===
"audio"
) {
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
user.audioTrack?.play();
}
2026-05-12 11:11:30 +08:00
}
);
2026-05-12 12:36:07 +08:00
// 用户离开
client.on(
"user-left",
(user) => {
console.log(
"用户离开:",
user.uid
);
if (
remoteVideoRef.current
) {
remoteVideoRef.current.innerHTML =
"";
}
}
);
// 加入频道
//await client.join(
// APP_ID,
// CHANNEL,
// TOKEN,
// uid
//);
2026-05-12 11:11:30 +08:00
await client.join(
appId,
channel,
token,
2026-05-12 11:11:30 +08:00
uid
);
2026-05-12 12:36:07 +08:00
console.log(
"加入频道成功"
);
setJoined(true);
2026-05-12 11:21:31 +08:00
message.success(
2026-05-12 12:36:07 +08:00
"RTC 已加入频道"
2026-05-12 11:11:30 +08:00
);
} catch (err) {
2026-05-12 12:36:07 +08:00
console.error(
"初始化失败:",
err
);
message.error(
"初始化失败"
);
2026-05-12 11:11:30 +08:00
}
};
init();
return () => {
client.leave();
};
2026-05-12 12:36:07 +08:00
}, [uid]);
2026-05-12 11:11:30 +08:00
2026-05-12 12:36:07 +08:00
// 开始推流
2026-05-12 11:11:30 +08:00
const startPublish =
async () => {
2026-05-12 12:36:07 +08:00
if (!joined) {
message.warning(
"RTC 还未加入频道"
);
return;
}
2026-05-12 11:11:30 +08:00
try {
2026-05-12 12:36:07 +08:00
// 麦克风轨道
2026-05-12 11:21:31 +08:00
const audioTrack =
2026-05-12 11:11:30 +08:00
await AgoraRTC.createMicrophoneAudioTrack();
2026-05-12 12:36:07 +08:00
// 摄像头轨道
2026-05-12 11:21:31 +08:00
const videoTrack =
await AgoraRTC.createCameraVideoTrack();
// 本地播放
videoTrack.play(
localVideoRef.current!
);
2026-05-12 12:36:07 +08:00
// 发布流
2026-05-12 11:11:30 +08:00
await client.publish([
2026-05-12 11:21:31 +08:00
audioTrack,
videoTrack,
2026-05-12 11:11:30 +08:00
]);
2026-05-12 12:36:07 +08:00
console.log(
"推流成功"
);
2026-05-12 11:21:31 +08:00
setPublished(true);
2026-05-12 11:11:30 +08:00
message.success(
2026-05-12 12:36:07 +08:00
"开始推流"
2026-05-12 11:11:30 +08:00
);
} catch (err) {
2026-05-12 12:36:07 +08:00
console.error(
"推流失败:",
err
);
2026-05-12 11:11:30 +08:00
message.error(
"推流失败"
);
}
};
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
ref={
remoteVideoRef
}
2026-05-12 11:11:30 +08:00
style={{
2026-05-12 11:21:31 +08:00
width: "100%",
height: '100%',
background:
"#000",
borderRadius: 8,
overflow:
"hidden",
2026-05-12 11:11:30 +08:00
}}
/>
2026-05-12 11:11:30 +08:00
</div>
2026-05-12 12:36:07 +08:00
2026-05-12 11:11:30 +08:00
);
}