75 lines
2.6 KiB
JavaScript
75 lines
2.6 KiB
JavaScript
// agent-renderer.js — 브라우저 환경에서 실행됨 → WebRTC 사용 가능
|
|
|
|
let peerConnection = null;
|
|
let socket = null;
|
|
let currentStream = null;
|
|
|
|
// 메인 프로세스로부터 명령 수신 (IPC)
|
|
console.log('electronAPI:', window.electronAPI);
|
|
console.log('desktopCapturer:', window.desktopCapturer);
|
|
window.electronAPI.receive('start-webrtc', async (payload) => {
|
|
try {
|
|
const { offer, displayId, signalingServer, employeeId } = payload;
|
|
console.log('start webrtc');
|
|
// 1. Socket 연결 (렌더러에서도 가능)
|
|
socket = io(signalingServer);
|
|
|
|
// 2. 화면 스트림 캡처
|
|
const sources = await window.desktopCapturer.getSources({ types: ['screen'] });
|
|
const source = sources.find(s => s.id === displayId);
|
|
if (!source) throw new Error('디스플레이 없음');
|
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({
|
|
audio: false,
|
|
video: {
|
|
mandatory: {
|
|
chromeMediaSource: 'desktop',
|
|
chromeMediaSourceId: source.id,
|
|
minWidth: 1280,
|
|
minHeight: 720
|
|
}
|
|
}
|
|
});
|
|
currentStream = stream;
|
|
|
|
// 3. WebRTC 연결
|
|
peerConnection = new RTCPeerConnection({
|
|
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
|
});
|
|
|
|
stream.getTracks().forEach(track => peerConnection.addTrack(track, stream));
|
|
|
|
await peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
|
|
const answer = await peerConnection.createAnswer();
|
|
await peerConnection.setLocalDescription(answer);
|
|
|
|
// 4. Answer 전송
|
|
socket.emit('webrtcSignal', {
|
|
targetId: employeeId,
|
|
answer
|
|
});
|
|
|
|
// 5. ICE candidate
|
|
peerConnection.onicecandidate = (e) => {
|
|
if (e.candidate) {
|
|
socket.emit('webrtcSignal', {
|
|
targetId: employeeId,
|
|
type: 'icecandidate', candidate: e.candidate
|
|
});
|
|
}
|
|
};
|
|
|
|
// 6. 입력 이벤트 수신 (robotjs는 메인 프로세스에서 실행 권장)
|
|
socket.on('inputEvent', (data) => {
|
|
window.electronApi.send('input-event-from-renderer', data);
|
|
});
|
|
|
|
console.log('✅ WebRTC 시작됨 (렌더러)');
|
|
} catch (err) {
|
|
console.error('❌ WebRTC 실패:', err);
|
|
window.electronApi.send('webrtc-error', err.message);
|
|
}
|
|
});
|
|
|
|
// desktopCapturer 사용을 위해 preload 필요
|
|
// → 다음 단계에서 preload.js 설정
|