Skip to content

Commit

Permalink
Fix: WebRTC 고유 번호 추가 (#104) (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
sayyyho authored Sep 10, 2024
1 parent cfbf0b2 commit 439ddc8
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/pages/WebRTC/VideoPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const RTCPage = () => {
const myVideoRef = useRef(null);
const remoteVideoRef = useRef(null);
const pcRef = useRef(null);
const roomName = params.roomName; // You can dynamically generate or pass this
const roomName = params.roomName;

const getMedia = async () => {
try {
Expand All @@ -22,8 +22,9 @@ const RTCPage = () => {

if (myVideoRef.current) {
myVideoRef.current.srcObject = stream;
myVideoRef.current.volume = 0; // 볼륨을 0으로 설정
myVideoRef.current.volume = 0;
}

stream.getTracks().forEach((track) => {
if (pcRef.current) {
pcRef.current.addTrack(track, stream);
Expand Down Expand Up @@ -61,7 +62,9 @@ const RTCPage = () => {

const createAnswer = async (offer) => {
try {
await pcRef.current.setRemoteDescription(offer);
await pcRef.current.setRemoteDescription(
new RTCSessionDescription(offer)
);
const answer = await pcRef.current.createAnswer();
await pcRef.current.setLocalDescription(answer);
console.log("Created answer: ", answer);
Expand Down Expand Up @@ -96,24 +99,30 @@ const RTCPage = () => {
socketRef.current.on("offer", async (offer) => {
console.log("Received offer: ", offer);
await getMedia();
createAnswer(offer);
await createAnswer(offer); // Ensure createAnswer is awaited
});

socketRef.current.on("answer", (answer) => {
socketRef.current.on("answer", async (answer) => {
console.log("Received answer: ", answer);
pcRef.current.setRemoteDescription(answer);
await pcRef.current.setRemoteDescription(
new RTCSessionDescription(answer)
);
});

socketRef.current.on("candidate", async (candidate) => {
console.log("Received candidate: ", candidate);
await pcRef.current.addIceCandidate(candidate);
try {
await pcRef.current.addIceCandidate(new RTCIceCandidate(candidate));
} catch (e) {
console.error("Error adding ICE candidate: ", e);
}
});

return () => {
if (socketRef.current) socketRef.current.disconnect();
if (pcRef.current) pcRef.current.close();
};
}, []);
}, [roomName]); // Add roomName to the dependency array to ensure updates

return (
<Layout>
Expand Down

0 comments on commit 439ddc8

Please sign in to comment.