ㄴㅇㄹ

This commit is contained in:
2025-09-20 00:17:30 +09:00
parent e75bb4777a
commit 161b18d10a

View File

@@ -16,7 +16,7 @@ export class WebRTCManager {
async init() { async init() {
try { try {
await this.acquireLocalStream(); // ✅ 이름도 의미에 맞게 변경 await this.acquireLocalStream(); // ✅ 이름도 의미에 맞게 변경gg
this.setupSignaling(); this.setupSignaling();
console.log('✅ WebRTC 매니저 초기화 완료'); console.log('✅ WebRTC 매니저 초기화 완료');
} catch (err) { } catch (err) {
@@ -58,6 +58,7 @@ export class WebRTCManager {
this.ws.onmessage = (event) => { this.ws.onmessage = (event) => {
const msg = JSON.parse(event.data); const msg = JSON.parse(event.data);
console.log('socket 메시지', msg)
if (msg.type === 'your-id') { if (msg.type === 'your-id') {
this.myId = msg.id; this.myId = msg.id;
console.log('내 ID:', this.myId); console.log('내 ID:', this.myId);
@@ -68,7 +69,9 @@ export class WebRTCManager {
this.addIceCandidateQueue.forEach(c => this.addIceCandidate(c)); this.addIceCandidateQueue.forEach(c => this.addIceCandidate(c));
this.addIceCandidateQueue = []; this.addIceCandidateQueue = [];
} }
this.peerId = msg.source
} else if (msg.type === 'answer') { } else if (msg.type === 'answer') {
console.log('answer:', this.myId);
this.setRemoteDescription(msg.sdp); this.setRemoteDescription(msg.sdp);
} else if (msg.type === 'candidate') { } else if (msg.type === 'candidate') {
this.addIceCandidate(msg.candidate); this.addIceCandidate(msg.candidate);
@@ -80,6 +83,7 @@ export class WebRTCManager {
}; };
} }
createPeerConnection() { createPeerConnection() {
if (!this.isInitialized || !this.localStream) { if (!this.isInitialized || !this.localStream) {
throw new Error('RTC가 초기화되지 않았거나 마이크 스트림이 없습니다.'); throw new Error('RTC가 초기화되지 않았거나 마이크 스트림이 없습니다.');
@@ -95,21 +99,37 @@ export class WebRTCManager {
this.localStream.getTracks().forEach(track => { this.localStream.getTracks().forEach(track => {
this.pc.addTrack(track, this.localStream); this.pc.addTrack(track, this.localStream);
console.log(this.pc);
}); });
this.pc.ontrack = (event) => { this.pc.ontrack = (event) => {
this.remoteStream = event.streams[0]; this.remoteStream = event.streams[0];
console.log('ontrack', this.remoteStream)
if (this.onRemoteStream) this.onRemoteStream(this.remoteStream); if (this.onRemoteStream) this.onRemoteStream(this.remoteStream);
}; };
this.onRemoteStream = (stream) => {
const audio = new Audio();
audio.muted = false;
audio.volume = 1;
audio.srcObject = stream;
audio.play().catch(e => console.warn('자동 재생 실패:', e));
console.log('음성실행')
}
this.pc.onicecandidate = (event) => { this.pc.onicecandidate = (event) => {
if (event.candidate && this.ws?.readyState === WebSocket.OPEN) { if (event.candidate) {
console.log('📤 ICE Candidate:', event.candidate.candidate);
if (this.ws?.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify({ this.ws.send(JSON.stringify({
target: this.peerId, target: this.peerId,
type: 'candidate', type: 'candidate',
candidate: event.candidate candidate: event.candidate
})); }));
} }
} else {
console.log('✅ ICE Candidate 수집 완료');
}
}; };
this.pc.oniceconnectionstatechange = () => { this.pc.oniceconnectionstatechange = () => {
@@ -131,22 +151,26 @@ export class WebRTCManager {
this.peerId = targetId; this.peerId = targetId;
this.createPeerConnection(); this.createPeerConnection();
const offer = await this.pc.createOffer(); const offer = await this.pc.createOffer();
await this.pc.setLocalDescription(offer); await this.pc.setLocalDescription(offer);
this.ws.send(JSON.stringify({ this.ws.send(JSON.stringify({
target: targetId, target: targetId,
source: this.myId,
type: 'offer', type: 'offer',
sdp: offer sdp: offer
})); }));
console.log('📤 Offer 전송 완료'); console.log('📤 Offer 전송 완료');
console.log('📤 Offer SDP:', offer.sdp);
} }
async setRemoteDescription(sdp) { async setRemoteDescription(sdp) {
// ✅ PeerConnection이 없으면 자동 생성 (수신 측 대응) // ✅ PeerConnection이 없으면 자동 생성 (수신 측 대응)
if (!this.pc) { if (!this.pc) {
this.createPeerConnection(); // ← pc 생성 + localStream 트랙 추가 if (!this.localStream) {
throw new Error('로컬 스트림이 준비되지 않았습니다. init()을 먼저 호출하세요.');
}
this.createPeerConnection();
} }
await this.pc.setRemoteDescription(new RTCSessionDescription(sdp)); await this.pc.setRemoteDescription(new RTCSessionDescription(sdp));
@@ -155,17 +179,20 @@ export class WebRTCManager {
const answer = await this.pc.createAnswer(); const answer = await this.pc.createAnswer();
await this.pc.setLocalDescription(answer); await this.pc.setLocalDescription(answer);
console.log(this.peerId)
this.ws.send(JSON.stringify({ this.ws.send(JSON.stringify({
target: this.peerId || 'unknown', target: this.peerId || 'unknown',
type: 'answer', type: 'answer',
sdp: answer sdp: answer
})); }));
console.log('📥 Answer 전송 완료'); console.log('📥 Answer 전송 완료', sdp);
console.log('로컬 스트림 트랙 수:', this.localStream.getTracks().length); console.log('로컬 스트림 트랙 수:', this.localStream.getTracks().length);
} }
} }
async addIceCandidate(candidate) { async addIceCandidate(candidate) {
if (this.pc && this.pc.remoteDescription) { if (this.pc && this.pc.remoteDescription) {
await this.pc.addIceCandidate(new RTCIceCandidate(candidate)); await this.pc.addIceCandidate(new RTCIceCandidate(candidate));