xg
This commit is contained in:
@@ -8,15 +8,12 @@ import AgoraRTC from "agora-rtc-sdk-ng";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Input,
|
|
||||||
Card,
|
Card,
|
||||||
Badge,
|
Badge,
|
||||||
message,
|
message,
|
||||||
Space,
|
Space,
|
||||||
} from "antd";
|
} from "antd";
|
||||||
|
|
||||||
import { io } from "socket.io-client";
|
|
||||||
|
|
||||||
const APP_ID =
|
const APP_ID =
|
||||||
"62a87b833b7e48888aca06b625f88c14";
|
"62a87b833b7e48888aca06b625f88c14";
|
||||||
|
|
||||||
@@ -30,10 +27,6 @@ const client = AgoraRTC.createClient({
|
|||||||
codec: "vp8",
|
codec: "vp8",
|
||||||
});
|
});
|
||||||
|
|
||||||
// websocket
|
|
||||||
const socket =
|
|
||||||
io("http://localhost:3001");
|
|
||||||
|
|
||||||
export default function AgoraRtcRoom() {
|
export default function AgoraRtcRoom() {
|
||||||
|
|
||||||
const [uid] = useState(() =>
|
const [uid] = useState(() =>
|
||||||
@@ -50,11 +43,8 @@ export default function AgoraRtcRoom() {
|
|||||||
const [published, setPublished] =
|
const [published, setPublished] =
|
||||||
useState(false);
|
useState(false);
|
||||||
|
|
||||||
const [msg, setMsg] =
|
const [joined, setJoined] =
|
||||||
useState("");
|
useState(false);
|
||||||
|
|
||||||
const [msgList, setMsgList] =
|
|
||||||
useState<string[]>([]);
|
|
||||||
|
|
||||||
const localVideoRef =
|
const localVideoRef =
|
||||||
useRef<HTMLDivElement>(null);
|
useRef<HTMLDivElement>(null);
|
||||||
@@ -68,30 +58,13 @@ export default function AgoraRtcRoom() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// websocket 加入房间
|
// RTC 状态监听
|
||||||
socket.emit(
|
|
||||||
"join-room",
|
|
||||||
CHANNEL
|
|
||||||
);
|
|
||||||
|
|
||||||
// 收消息
|
|
||||||
socket.on(
|
|
||||||
"receive-message",
|
|
||||||
(data) => {
|
|
||||||
|
|
||||||
setMsgList(prev => [
|
|
||||||
...prev,
|
|
||||||
`${data.uid}: ${data.message}`
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// rtc 状态
|
|
||||||
client.on(
|
client.on(
|
||||||
"connection-state-change",
|
"connection-state-change",
|
||||||
(state) => {
|
(state) => {
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
|
"RTC状态:",
|
||||||
state
|
state
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -103,11 +76,16 @@ export default function AgoraRtcRoom() {
|
|||||||
setConnected(
|
setConnected(
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
setConnected(
|
||||||
|
false
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// 远端用户发布
|
// 远端用户发布流
|
||||||
client.on(
|
client.on(
|
||||||
"user-published",
|
"user-published",
|
||||||
async (
|
async (
|
||||||
@@ -116,11 +94,12 @@ export default function AgoraRtcRoom() {
|
|||||||
) => {
|
) => {
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"远端发布",
|
"远端用户发布:",
|
||||||
user.uid,
|
user.uid,
|
||||||
mediaType
|
mediaType
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 订阅远端流
|
||||||
await client.subscribe(
|
await client.subscribe(
|
||||||
user,
|
user,
|
||||||
mediaType
|
mediaType
|
||||||
@@ -148,7 +127,27 @@ export default function AgoraRtcRoom() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// 加入 rtc
|
// 用户离开
|
||||||
|
client.on(
|
||||||
|
"user-left",
|
||||||
|
(user) => {
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"用户离开:",
|
||||||
|
user.uid
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
remoteVideoRef.current
|
||||||
|
) {
|
||||||
|
|
||||||
|
remoteVideoRef.current.innerHTML =
|
||||||
|
"";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 加入频道
|
||||||
await client.join(
|
await client.join(
|
||||||
APP_ID,
|
APP_ID,
|
||||||
CHANNEL,
|
CHANNEL,
|
||||||
@@ -156,15 +155,26 @@ export default function AgoraRtcRoom() {
|
|||||||
uid
|
uid
|
||||||
);
|
);
|
||||||
|
|
||||||
setConnected(true);
|
console.log(
|
||||||
|
"加入频道成功"
|
||||||
|
);
|
||||||
|
|
||||||
|
setJoined(true);
|
||||||
|
|
||||||
message.success(
|
message.success(
|
||||||
"RTC 已连接"
|
"RTC 已加入频道"
|
||||||
);
|
);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
||||||
console.error(err);
|
console.error(
|
||||||
|
"初始化失败:",
|
||||||
|
err
|
||||||
|
);
|
||||||
|
|
||||||
|
message.error(
|
||||||
|
"初始化失败"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -173,23 +183,30 @@ export default function AgoraRtcRoom() {
|
|||||||
return () => {
|
return () => {
|
||||||
|
|
||||||
client.leave();
|
client.leave();
|
||||||
|
|
||||||
socket.disconnect();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}, []);
|
}, [uid]);
|
||||||
|
|
||||||
// 推流
|
// 开始推流
|
||||||
const startPublish =
|
const startPublish =
|
||||||
async () => {
|
async () => {
|
||||||
|
|
||||||
|
if (!joined) {
|
||||||
|
|
||||||
|
message.warning(
|
||||||
|
"RTC 还未加入频道"
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// 创建音频轨道
|
// 麦克风轨道
|
||||||
const audioTrack =
|
const audioTrack =
|
||||||
await AgoraRTC.createMicrophoneAudioTrack();
|
await AgoraRTC.createMicrophoneAudioTrack();
|
||||||
|
|
||||||
// 创建视频轨道
|
// 摄像头轨道
|
||||||
const videoTrack =
|
const videoTrack =
|
||||||
await AgoraRTC.createCameraVideoTrack();
|
await AgoraRTC.createCameraVideoTrack();
|
||||||
|
|
||||||
@@ -198,21 +215,28 @@ export default function AgoraRtcRoom() {
|
|||||||
localVideoRef.current!
|
localVideoRef.current!
|
||||||
);
|
);
|
||||||
|
|
||||||
// 发布
|
// 发布流
|
||||||
await client.publish([
|
await client.publish([
|
||||||
audioTrack,
|
audioTrack,
|
||||||
videoTrack,
|
videoTrack,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"推流成功"
|
||||||
|
);
|
||||||
|
|
||||||
setPublished(true);
|
setPublished(true);
|
||||||
|
|
||||||
message.success(
|
message.success(
|
||||||
"推流成功"
|
"开始推流"
|
||||||
);
|
);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
||||||
console.error(err);
|
console.error(
|
||||||
|
"推流失败:",
|
||||||
|
err
|
||||||
|
);
|
||||||
|
|
||||||
message.error(
|
message.error(
|
||||||
"推流失败"
|
"推流失败"
|
||||||
@@ -220,30 +244,10 @@ export default function AgoraRtcRoom() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 发消息
|
|
||||||
const sendMessage =
|
|
||||||
() => {
|
|
||||||
|
|
||||||
if (!msg) return;
|
|
||||||
|
|
||||||
socket.emit(
|
|
||||||
"send-message",
|
|
||||||
{
|
|
||||||
roomId:
|
|
||||||
CHANNEL,
|
|
||||||
uid,
|
|
||||||
message:
|
|
||||||
msg,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
setMsg("");
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
||||||
<Card
|
<Card
|
||||||
title="Agora RTC 音视频 + 聊天"
|
title="Agora RTC 音视频测试"
|
||||||
style={{
|
style={{
|
||||||
width: 1000,
|
width: 1000,
|
||||||
margin:
|
margin:
|
||||||
@@ -259,7 +263,7 @@ export default function AgoraRtcRoom() {
|
|||||||
>
|
>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
UID:{uid}
|
当前 UID:{uid}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Badge
|
<Badge
|
||||||
@@ -275,12 +279,26 @@ export default function AgoraRtcRoom() {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<Badge
|
||||||
|
color={
|
||||||
|
published
|
||||||
|
? "green"
|
||||||
|
: "orange"
|
||||||
|
}
|
||||||
|
text={
|
||||||
|
published
|
||||||
|
? "已推流"
|
||||||
|
: "未推流"
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
type="primary"
|
type="primary"
|
||||||
onClick={
|
onClick={
|
||||||
startPublish
|
startPublish
|
||||||
}
|
}
|
||||||
disabled={
|
disabled={
|
||||||
|
!joined ||
|
||||||
published
|
published
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
@@ -291,6 +309,7 @@ export default function AgoraRtcRoom() {
|
|||||||
style={{
|
style={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
gap: 20,
|
gap: 20,
|
||||||
|
marginTop: 20,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|
||||||
@@ -305,10 +324,13 @@ export default function AgoraRtcRoom() {
|
|||||||
localVideoRef
|
localVideoRef
|
||||||
}
|
}
|
||||||
style={{
|
style={{
|
||||||
width: 320,
|
width: 400,
|
||||||
height: 240,
|
height: 300,
|
||||||
background:
|
background:
|
||||||
"#000",
|
"#000",
|
||||||
|
borderRadius: 8,
|
||||||
|
overflow:
|
||||||
|
"hidden",
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@@ -325,10 +347,13 @@ export default function AgoraRtcRoom() {
|
|||||||
remoteVideoRef
|
remoteVideoRef
|
||||||
}
|
}
|
||||||
style={{
|
style={{
|
||||||
width: 320,
|
width: 400,
|
||||||
height: 240,
|
height: 300,
|
||||||
background:
|
background:
|
||||||
"#000",
|
"#000",
|
||||||
|
borderRadius: 8,
|
||||||
|
overflow:
|
||||||
|
"hidden",
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@@ -336,70 +361,6 @@ export default function AgoraRtcRoom() {
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
|
||||||
|
|
||||||
<h3>
|
|
||||||
聊天
|
|
||||||
</h3>
|
|
||||||
|
|
||||||
<Space>
|
|
||||||
|
|
||||||
<Input
|
|
||||||
value={msg}
|
|
||||||
onChange={e =>
|
|
||||||
setMsg(
|
|
||||||
e.target.value
|
|
||||||
)
|
|
||||||
}
|
|
||||||
style={{
|
|
||||||
width: 300,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
onClick={
|
|
||||||
sendMessage
|
|
||||||
}
|
|
||||||
>
|
|
||||||
发送
|
|
||||||
</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>
|
</Space>
|
||||||
|
|
||||||
</Card>
|
</Card>
|
||||||
|
|||||||
Reference in New Issue
Block a user