From 1847f25fa38969ab0dff971a5d5051639286e12f Mon Sep 17 00:00:00 2001 From: bangae1 Date: Tue, 21 Oct 2025 14:35:15 +0900 Subject: [PATCH] 123 --- agent-renderer.js | 51 ++++++++++++++++++++++------------------------- main.js | 51 ++++++++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 50 deletions(-) diff --git a/agent-renderer.js b/agent-renderer.js index 75e670e..031ae4b 100644 --- a/agent-renderer.js +++ b/agent-renderer.js @@ -2,7 +2,7 @@ let peerConnection = null; let currentStream = null; - +let connection = false; // 메인 프로세스로부터 명령 수신 (IPC) console.log('electronAPI:', window.electronAPI); console.log('desktopCapturer:', window.desktopCapturer); @@ -33,41 +33,38 @@ window.electronAPI.receive('start-webrtc', async (data) => { } } }); + + await peerConnection.setRemoteDescription(new RTCSessionDescription(data.offer)); currentStream = stream; stream.getTracks().forEach(track => { console.log('track', track, stream); peerConnection.addTrack(track, stream) }); - - const offer = await peerConnection.createOffer({offerToReceiveVideo: true}) - await peerConnection.setLocalDescription(offer); - window.electronAPI.send('offer', {type:'offer', targetId: data.targetId, offer}) -}); - -window.electronAPI.receive('displays', async (payload) => { - window.desktopCapturer.getSources(['display']) -}); + const answer = await peerConnection.createAnswer(); + await peerConnection.setLocalDescription(answer); + connection = true; -window.electronAPI.receive('answer', async (data) => { - console.log('answer', data) - await peerConnection.setRemoteDescription(new RTCSessionDescription(data.answer)); - console.log('connect') -}) + window.electronAPI.send('requestAnswer', {type: 'answer', targetId: data.targetId, answer}); -window.electronAPI.receive('webrtcSignal', async (data) => { - try { - console.log('webrtcSignal', data) - if (!peerConnection) return; - if (data.type === 'icecandidate' && data.candidate) { - await peerConnection.addIceCandidate(new RTCIceCandidate(data.candidate)); - } else if(data.type === 'answer') { - await peerConnection.setRemoteDescription(new RTCSessionDescription(data.answer)); - } - } catch (err) { - console.error('ICE candidate 처리 오류:', err); + peerConnection.onicecandidate = (event) => { + setTimeout(() => { + console.log('icecandidate', event); + window.electronAPI.send('icecandidate', {type: 'icecandidate', targetId: data.targetId, server:'agent', 'candidate': event.candidate}); + }, 2000) } +}); + +window.electronAPI.receive('displays', async (data) => { + await window.desktopCapturer.getSources(['display']) +}); + +window.electronAPI.receive('icecandidate', async (data) => { + if(connection) { + console.log(data.candidate) + await peerConnection.addIceCandidate(new RTCIceCandidate(data.candidate)); + } +}); -}) // desktopCapturer 사용을 위해 preload 필요 // → 다음 단계에서 preload.js 설정 \ No newline at end of file diff --git a/main.js b/main.js index 353113b..bd47428 100644 --- a/main.js +++ b/main.js @@ -113,9 +113,21 @@ function connectToSignaling() { }); - /*연결 요청 -> webrtc 시작 -> offer 전송*/ - socket.on('responseControl', async (data) => { - console.log(data) + // WebRTC 신호 수신 (ICE candidate 등) + socket.on('icecandidate', async (data) => { + rendererWindow.webContents.send('icecandidate', data); + }); + + socket.on('disconnect', () => { + console.log('시그널링 서버와 연결 끊김'); + if (peerConnection) { + peerConnection.close(); + peerConnection = null; + } + }); + + socket.on('responseOffer', async (data) => { + console.log('responseOffer', data); const result = await dialog.showMessageBox({ type: 'question', title: '원격 연결 요청', @@ -130,31 +142,14 @@ function connectToSignaling() { rendererWindow.webContents.send('start-webrtc', { displayId: data.displayId, targetId: data.targetId, + offer: data.offer, sources }); } else { socket.emit('webrtcSignal', { targetId: data.targetId, data: { type: 'reject' } }); } - }); - - socket.on('answer', async (data) => { - rendererWindow.webContents.send('answer', data); - console.log('answer', data); }) - // WebRTC 신호 수신 (ICE candidate 등) - socket.on('webrtcSignal', async (data) => { - rendererWindow.webContents.send('webrtcSignal', data); - }); - - socket.on('disconnect', () => { - console.log('시그널링 서버와 연결 끊김'); - if (peerConnection) { - peerConnection.close(); - peerConnection = null; - } - }); - socket.on('error', (err) => { console.error('소켓 오류:', err); }); @@ -196,6 +191,16 @@ ipcMain.on('input-event-from-renderer', (event, data) => { }) -ipcMain.on('offer', (event, data) => { - socket.emit('offer', data); +ipcMain.on('requestAnswer', (event, data) => { + socket.emit('requestAnswer', data); +}) + + +ipcMain.on('icecandidate', (event, data) => { + socket.emit('icecandidate', data); +}) + +ipcMain.on('start', (event, data) => { + console.log('start', data); + socket.emit('start', data); }) \ No newline at end of file