Aa
This commit is contained in:
98
main.js
98
main.js
@@ -14,6 +14,7 @@ let tray = null;
|
||||
let mainWindow = null;
|
||||
let socket = null;
|
||||
let peerConnection = null;
|
||||
let displayId = null;
|
||||
|
||||
// 자동 시작 설정
|
||||
app.setLoginItemSettings({
|
||||
@@ -53,6 +54,9 @@ function createTray() {
|
||||
async function sendAvailableDisplays() {
|
||||
try {
|
||||
const sources = await desktopCapturer.getSources({ types: ['screen'] });
|
||||
const source = sources[0];
|
||||
displayId = source.id
|
||||
console.log('sources',sources)
|
||||
const displays = sources.map(source => ({
|
||||
id: source.id,
|
||||
name: source.name,
|
||||
@@ -65,76 +69,6 @@ async function sendAvailableDisplays() {
|
||||
}
|
||||
}
|
||||
|
||||
// 🔁 WebRTC 연결 설정 (offer 수신 후 호출)
|
||||
async function setupWebRTCConnection(offer, requestedDisplayId) {
|
||||
try {
|
||||
// 기존 연결 정리
|
||||
if (peerConnection) {
|
||||
peerConnection.close();
|
||||
}
|
||||
|
||||
peerConnection = new RTCPeerConnection({
|
||||
iceServers: [
|
||||
{ urls: 'stun:stun.l.google.com:19302' }
|
||||
// 프로덕션: TURN 서버 추가 권장
|
||||
]
|
||||
});
|
||||
|
||||
// 🖼️ 화면 스트림 캡처
|
||||
const sources = await desktopCapturer.getSources({ types: ['screen'] });
|
||||
const source = sources.find(s => s.id === requestedDisplayId);
|
||||
if (!source) throw new Error(`디스플레이 ${requestedDisplayId}를 찾을 수 없습니다.`);
|
||||
|
||||
const stream = await navigator.mediaDevices.getUserMedia({
|
||||
audio: false,
|
||||
video: {
|
||||
mandatory: {
|
||||
chromeMediaSource: 'desktop',
|
||||
chromeMediaSourceId: source.id,
|
||||
minWidth: 1280,
|
||||
minHeight: 720,
|
||||
maxWidth: 1920,
|
||||
maxHeight: 1080
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 스트림을 PeerConnection에 추가
|
||||
stream.getTracks().forEach(track => {
|
||||
peerConnection.addTrack(track, stream);
|
||||
});
|
||||
|
||||
// Offer 설정
|
||||
await peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
|
||||
|
||||
// ✅ Answer SDP 생성
|
||||
const answer = await peerConnection.createAnswer();
|
||||
await peerConnection.setLocalDescription(answer);
|
||||
|
||||
// Answer 전송
|
||||
socket.emit('webrtcSignal', {
|
||||
targetId: EMPLOYEE_ID,
|
||||
answer
|
||||
});
|
||||
|
||||
// ICE candidate 전송
|
||||
peerConnection.onicecandidate = (event) => {
|
||||
if (event.candidate) {
|
||||
socket.emit('webrtcSignal', {
|
||||
targetId: EMPLOYEE_ID,
|
||||
type: 'icecandidate',
|
||||
candidate: event.candidate
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
console.log('✅ WebRTC 연결 설정 완료. 화면 공유 시작됨.');
|
||||
} catch (err) {
|
||||
console.error('❌ WebRTC 설정 실패:', err);
|
||||
dialog.showErrorBox('오류', '화면 공유 시작에 실패했습니다:\n' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 🖱️ 입력 이벤트 처리 (robotjs 필요)
|
||||
function setupInputHandler() {
|
||||
// robotjs는 별도 설치 및 권한 필요
|
||||
@@ -175,11 +109,11 @@ function connectToSignaling() {
|
||||
sendAvailableDisplays(); // 연결 시 디스플레이 목록 전송
|
||||
});
|
||||
|
||||
socket.on('controlRequest', async ({ offer, displayId }) => {
|
||||
socket.on('controlRequest', async (data) => {
|
||||
const result = await dialog.showMessageBox({
|
||||
type: 'question',
|
||||
title: '원격 연결 요청',
|
||||
message: `관리자로부터 원격 제어 요청이 왔습니다.\n허용하시겠습니까?`,
|
||||
message: `관리자로부터 원격 제어 요청이 왔습니다.\n허용하시겠습니까? ${displayId}`,
|
||||
buttons: ['허용', '거부'],
|
||||
defaultId: 1,
|
||||
cancelId: 1
|
||||
@@ -188,26 +122,19 @@ function connectToSignaling() {
|
||||
if (result.response === 0) {
|
||||
// ✅ 렌더러 프로세스에 WebRTC 시작 명령 전달
|
||||
rendererWindow.webContents.send('start-webrtc', {
|
||||
offer,
|
||||
offer: data.offer,
|
||||
displayId,
|
||||
signalingServer: SIGNALING_SERVER,
|
||||
employeeId: EMPLOYEE_ID
|
||||
targetId: data.from
|
||||
});
|
||||
} else {
|
||||
socket.emit('webrtcSignal', { targetId: EMPLOYEE_ID, data: { type: 'reject' } });
|
||||
socket.emit('webrtcSignal', { targetId: data.from, data: { type: 'reject' } });
|
||||
}
|
||||
});
|
||||
|
||||
// WebRTC 신호 수신 (ICE candidate 등)
|
||||
socket.on('webrtcSignal', async ({ data }) => {
|
||||
if (!peerConnection) return;
|
||||
try {
|
||||
if (data.type === 'icecandidate' && data.candidate) {
|
||||
await peerConnection.addIceCandidate(new RTCIceCandidate(data.candidate));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('ICE candidate 처리 오류:', err);
|
||||
}
|
||||
socket.on('webrtcSignal', async (data) => {
|
||||
rendererWindow.webContents.send('webrtcSignal', data);
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
@@ -255,4 +182,5 @@ ipcMain.on('input-event-from-renderer', (event, data) => {
|
||||
} catch (e) {
|
||||
console.error('robotjs 오류:', e);
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user