86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
// agent-renderer.js — 브라우저 환경에서 실행됨 → WebRTC 사용 가능
|
|
|
|
let peerConnection = null;
|
|
let currentStream = null;
|
|
let dataChannel = null;
|
|
let connection = false;
|
|
// 메인 프로세스로부터 명령 수신 (IPC)
|
|
console.log('electronAPI:', window.electronAPI);
|
|
console.log('desktopCapturer:', window.desktopCapturer);
|
|
|
|
window.electronAPI.send('displays', { types: ['screen'] });
|
|
|
|
console.log('start webrtc');
|
|
// 1. Socket 연결 (렌더러에서도 가능)
|
|
|
|
window.electronAPI.receive('start-webrtc', async (data) => {
|
|
const source = data.sources.find(s => {
|
|
return s.id === data.displayId
|
|
});
|
|
|
|
// 3. WebRTC 연결
|
|
peerConnection = new RTCPeerConnection({
|
|
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
|
|
});
|
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({
|
|
audio: false,
|
|
video: {
|
|
mandatory: {
|
|
chromeMediaSource: 'desktop',
|
|
chromeMediaSourceId: source.id,
|
|
minWidth: 1920,
|
|
minHeight: 1080
|
|
}
|
|
}
|
|
});
|
|
|
|
await peerConnection.setRemoteDescription(new RTCSessionDescription(data.offer));
|
|
currentStream = stream;
|
|
stream.getTracks().forEach(track => {
|
|
console.log('track', track, stream);
|
|
peerConnection.addTrack(track, stream)
|
|
});
|
|
const answer = await peerConnection.createAnswer();
|
|
await peerConnection.setLocalDescription(answer);
|
|
connection = true;
|
|
|
|
|
|
window.electronAPI.send('requestAnswer', {type: 'answer', targetId: data.targetId, answer});
|
|
|
|
peerConnection.onicecandidate = (event) => {
|
|
setTimeout(() => {
|
|
console.log('icecandidate', event);
|
|
window.electronAPI.send('icecandidate', {type: 'icecandidate', targetId: data.targetId, server:'agent', 'candidate': event.candidate});
|
|
}, 2000)
|
|
}
|
|
|
|
peerConnection.ondatachannel = (event) => {
|
|
console.log('datachannel', event);
|
|
dataChannel = event.channel;
|
|
dataChannel.onmessage = (event) => {
|
|
window.electronAPI.send('inputEvent', JSON.parse(event.data))
|
|
}
|
|
}
|
|
|
|
|
|
});
|
|
|
|
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));
|
|
}
|
|
});
|
|
|
|
window.electronAPI.receive('disconnect', async (data) => {
|
|
peerConnection.close();
|
|
peerConnection = null;
|
|
});
|
|
|
|
// desktopCapturer 사용을 위해 preload 필요
|
|
// → 다음 단계에서 preload.js 설정
|