问题描述
我正在使用 agora SDK 与一位用户进行实时连接。我知道该用户何时上线,但无法连接。以下是代码参考:
function App() {
const [joined,setJoined] = useState(false);
const channelRef = useRef("");
const remoteRef = useRef("");
const leaveRef = useRef("");
async function handleSubmit(e) {
try {
if (channelRef.current.value === "") {
return console.log("Please Enter Channel Name");
}
setJoined(true);
rtc.client = AgoraRTC.createClient({ mode: "live",codec: "vp8" })
await rtc.client.join(
options.appId,options.channel,options.token || null,options.uid || null,);
// Create an audio track from the audio captured by a microphone
rtc.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack();
// Create a video track from the video captured by a camera
rtc.localVideoTrack = await AgoraRTC.createCameravideoTrack();
rtc.localVideoTrack.play("local-stream");
rtc.client.on("user-published",async (user,mediaType) => {
// Subscribe to a remote user
await rtc.client.subscribe(user,mediaType);
console.log("subscribe success");
// console.log(user);
if (mediaType === "video" || mediaType === "all") {
// Get `RemoteVideoTrack` in the `user` object.
const remoteVideoTrack = user.videoTrack;
console.log(remoteVideoTrack);
// Dynamically create a container in the form of a DIV element for playing the remote video track.
const PlayerContainer = React.createElement("div",{
id: user.uid,className: "stream",});
ReactDOM.render(
PlayerContainer,document.getElementById("remote-stream")
);
user.videoTrack.play(`${user.uid}`);
}
if (mediaType === "audio" || mediaType === "all") {
// Get `RemoteAudioTrack` in the `user` object.
const remoteAudioTrack = user.audioTrack;
// Play the audio track. Do not need to pass any DOM element
remoteAudioTrack.play();
}
});
rtc.client.on("user-unpublished",(user) => {
// Get the dynamically created DIV container
const playerContainer = document.getElementById(user.uid);
console.log(playerContainer);
});
// Publish the local audio and video tracks to the channel
await rtc.client.publish([rtc.localAudioTrack,rtc.localVideoTrack]);
console.log("publish success!");
} catch (error) {
console.error(error);
}
}
async function handleLeave() {
try {
const localContainer = document.getElementById("local-stream");
rtc.localAudioTrack.close();
rtc.localVideoTrack.close();
setJoined(false);
localContainer.textContent = "";
// Traverse all remote users
rtc.client.remoteUsers.forEach((user) => {
// Destroy the dynamically created DIV container
const playerContainer = document.getElementById(user.uid);
playerContainer && playerContainer.remove();
});
// Leave the channel
await rtc.client.leave();
} catch (err) {
console.error(err);
}
}
return (
<>
<div className="container">
<input
type="submit"
value="Join"
onClick={handleSubmit}
disabled={joined ? true : false}
/>
<input
type="button"
ref={leaveRef}
value="Leave"
onClick={handleLeave}
disabled={joined ? false : true}
/>
</div>
{joined ? (
<>
<div id="local-stream" className="stream local-stream"></div>
<div
id="remote-stream"
ref={remoteRef}
className="stream remote-stream"
></div>
</>
) : null}
</>
);
}
export default App;
我可以连接,但是它显示了我这边的视频,这是意料之外的。我应该看到另一边的视频直播。那么需要进行哪些代码更改才能启用实时会话?
解决方法
直播时需要分配不同的客户端角色。 请参阅 this 以供参考
您需要通过
将其初始化为“host”rtc.client = AgoraRTC.createClient({ mode: "live",codec: "vp8" })
rtc.client.setClientRole("host")
await rtc.client.join(
options.appId,options.channel,options.token || null,options.uid || null,);
HTH!