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

408 lines
5.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,
2026-05-12 11:21:31 +08:00
Input,
2026-05-12 11:11:30 +08:00
Card,
Badge,
message,
2026-05-12 11:21:31 +08:00
Space,
2026-05-12 11:11:30 +08:00
} from "antd";
2026-05-12 11:21:31 +08:00
import { io } from "socket.io-client";
2026-05-12 11:11:30 +08:00
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",
});
2026-05-12 11:21:31 +08:00
// websocket
const socket =
io("http://localhost:3001");
export default function AgoraRtcRoom() {
2026-05-12 11:11:30 +08:00
const [uid] = useState(() =>
2026-05-12 11:21:31 +08:00
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 11:21:31 +08:00
const [msg, setMsg] =
useState("");
const [msgList, setMsgList] =
useState<string[]>([]);
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 11:21:31 +08:00
// websocket 加入房间
socket.emit(
"join-room",
CHANNEL
);
// 收消息
socket.on(
"receive-message",
(data) => {
setMsgList(prev => [
...prev,
`${data.uid}: ${data.message}`
]);
}
);
// rtc 状态
2026-05-12 11:11:30 +08:00
client.on(
"connection-state-change",
(state) => {
console.log(
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
);
}
}
);
// 远端用户发布
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 11:21:31 +08:00
"远端发布",
2026-05-12 11:11:30 +08:00
user.uid,
mediaType
);
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 11:21:31 +08:00
// 加入 rtc
2026-05-12 11:11:30 +08:00
await client.join(
APP_ID,
CHANNEL,
TOKEN,
uid
);
2026-05-12 11:21:31 +08:00
setConnected(true);
message.success(
"RTC 已连接"
2026-05-12 11:11:30 +08:00
);
} catch (err) {
console.error(err);
}
};
init();
return () => {
client.leave();
2026-05-12 11:21:31 +08:00
socket.disconnect();
2026-05-12 11:11:30 +08:00
};
}, []);
// 推流
const startPublish =
async () => {
try {
2026-05-12 11:21:31 +08:00
// 创建音频轨道
const audioTrack =
2026-05-12 11:11:30 +08:00
await AgoraRTC.createMicrophoneAudioTrack();
2026-05-12 11:21:31 +08:00
// 创建视频轨道
const videoTrack =
await AgoraRTC.createCameraVideoTrack();
// 本地播放
videoTrack.play(
localVideoRef.current!
);
// 发布
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 11:21:31 +08:00
setPublished(true);
2026-05-12 11:11:30 +08:00
message.success(
2026-05-12 11:21:31 +08:00
"推流成功"
2026-05-12 11:11:30 +08:00
);
} catch (err) {
2026-05-12 11:21:31 +08:00
console.error(err);
2026-05-12 11:11:30 +08:00
message.error(
"推流失败"
);
}
};
2026-05-12 11:21:31 +08:00
// 发消息
const sendMessage =
() => {
if (!msg) return;
socket.emit(
"send-message",
{
roomId:
CHANNEL,
uid,
message:
msg,
}
);
setMsg("");
};
2026-05-12 11:11:30 +08:00
return (
<Card
2026-05-12 11:21:31 +08:00
title="Agora RTC 音视频 + 聊天"
2026-05-12 11:11:30 +08:00
style={{
2026-05-12 11:21:31 +08:00
width: 1000,
margin:
"20px auto",
2026-05-12 11:11:30 +08:00
}}
>
2026-05-12 11:21:31 +08:00
<Space
direction="vertical"
2026-05-12 11:11:30 +08:00
style={{
2026-05-12 11:21:31 +08:00
width: "100%",
2026-05-12 11:11:30 +08:00
}}
>
2026-05-12 11:21:31 +08:00
<div>
UID:{uid}
</div>
2026-05-12 11:11:30 +08:00
<Badge
color={
connected
? "green"
: "red"
}
text={
connected
? "RTC 已连接"
: "RTC 未连接"
}
/>
2026-05-12 11:21:31 +08:00
<Button
type="primary"
onClick={
startPublish
2026-05-12 11:11:30 +08:00
}
2026-05-12 11:21:31 +08:00
disabled={
2026-05-12 11:11:30 +08:00
published
}
2026-05-12 11:21:31 +08:00
>
开始音视频推流
</Button>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<div
style={{
display: "flex",
gap: 20,
}}
>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<div>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<h3>
本地视频
</h3>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<div
ref={
localVideoRef
}
style={{
width: 320,
height: 240,
background:
"#000",
}}
/>
</div>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<div>
<h3>
远端视频
</h3>
2026-05-12 11:11:30 +08:00
<div
2026-05-12 11:21:31 +08:00
ref={
remoteVideoRef
}
style={{
width: 320,
height: 240,
background:
"#000",
}}
/>
</div>
</div>
<div>
<h3>
聊天
</h3>
<Space>
<Input
value={msg}
onChange={e =>
setMsg(
e.target.value
)
}
style={{
width: 300,
}}
/>
<Button
onClick={
sendMessage
}
2026-05-12 11:11:30 +08:00
>
2026-05-12 11:21:31 +08:00
发送
</Button>
</Space>
<div
style={{
marginTop: 20,
height: 200,
border:
"1px solid #ddd",
padding: 10,
overflowY:
"auto",
}}
>
{msgList.map(
(
item,
index
) => (
<div
key={
index
}
>
{
item
}
</div>
)
)}
</div>
</div>
</Space>
2026-05-12 11:11:30 +08:00
</Card>
);
}