This commit is contained in:
bangae1
2025-10-21 14:35:15 +09:00
parent b0f09e0a1f
commit 1847f25fa3
2 changed files with 52 additions and 50 deletions

View File

@@ -2,7 +2,7 @@
let peerConnection = null; let peerConnection = null;
let currentStream = null; let currentStream = null;
let connection = false;
// 메인 프로세스로부터 명령 수신 (IPC) // 메인 프로세스로부터 명령 수신 (IPC)
console.log('electronAPI:', window.electronAPI); console.log('electronAPI:', window.electronAPI);
console.log('desktopCapturer:', window.desktopCapturer); 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; currentStream = stream;
stream.getTracks().forEach(track => { stream.getTracks().forEach(track => {
console.log('track', track, stream); console.log('track', track, stream);
peerConnection.addTrack(track, stream) peerConnection.addTrack(track, stream)
}); });
const answer = await peerConnection.createAnswer();
const offer = await peerConnection.createOffer({offerToReceiveVideo: true}) await peerConnection.setLocalDescription(answer);
await peerConnection.setLocalDescription(offer); connection = true;
window.electronAPI.send('offer', {type:'offer', targetId: data.targetId, offer})
});
window.electronAPI.receive('displays', async (payload) => {
window.desktopCapturer.getSources(['display'])
});
window.electronAPI.receive('answer', async (data) => { window.electronAPI.send('requestAnswer', {type: 'answer', targetId: data.targetId, answer});
console.log('answer', data)
await peerConnection.setRemoteDescription(new RTCSessionDescription(data.answer));
console.log('connect')
})
window.electronAPI.receive('webrtcSignal', async (data) => { peerConnection.onicecandidate = (event) => {
try { setTimeout(() => {
console.log('webrtcSignal', data) console.log('icecandidate', event);
if (!peerConnection) return; window.electronAPI.send('icecandidate', {type: 'icecandidate', targetId: data.targetId, server:'agent', 'candidate': event.candidate});
if (data.type === 'icecandidate' && data.candidate) { }, 2000)
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);
} }
});
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 필요 // desktopCapturer 사용을 위해 preload 필요
// → 다음 단계에서 preload.js 설정 // → 다음 단계에서 preload.js 설정

51
main.js
View File

@@ -113,9 +113,21 @@ function connectToSignaling() {
}); });
/*연결 요청 -> webrtc 시작 -> offer 전송*/ // WebRTC 신호 수신 (ICE candidate 등)
socket.on('responseControl', async (data) => { socket.on('icecandidate', async (data) => {
console.log(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({ const result = await dialog.showMessageBox({
type: 'question', type: 'question',
title: '원격 연결 요청', title: '원격 연결 요청',
@@ -130,31 +142,14 @@ function connectToSignaling() {
rendererWindow.webContents.send('start-webrtc', { rendererWindow.webContents.send('start-webrtc', {
displayId: data.displayId, displayId: data.displayId,
targetId: data.targetId, targetId: data.targetId,
offer: data.offer,
sources sources
}); });
} else { } else {
socket.emit('webrtcSignal', { targetId: data.targetId, data: { type: 'reject' } }); 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) => { socket.on('error', (err) => {
console.error('소켓 오류:', err); console.error('소켓 오류:', err);
}); });
@@ -196,6 +191,16 @@ ipcMain.on('input-event-from-renderer', (event, data) => {
}) })
ipcMain.on('offer', (event, data) => { ipcMain.on('requestAnswer', (event, data) => {
socket.emit('offer', 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);
}) })