This commit is contained in:
2025-10-20 23:01:53 +09:00
parent 29153400e6
commit 8644fb1c46

View File

@@ -26,13 +26,16 @@ export default function RemoteControl() {
socket.on('connect', () => { socket.on('connect', () => {
console.log('시그널링 서버 연결됨. 직원 등록 중...'); console.log('시그널링 서버 연결됨. 직원 등록 중...');
socket.emit('register', EMPLOYEE_ID); socket.emit('register', EMPLOYEE_ID);
socket.emit('requestDisplays', employeeId)
}); });
socket.on('availableDisplays', ({ displays }) => {
socket.on('responseDisplays', ({ displays }) => {
setDisplays(displays); setDisplays(displays);
if (displays.length > 0 && !selectedDisplay) { if (displays.length > 0 && !selectedDisplay) {
setSelectedDisplay(displays[0].id); setSelectedDisplay(displays[0].id);
} }
console.log(displays);
}); });
socket.on('webrtcSignal', async (data) => { socket.on('webrtcSignal', async (data) => {
@@ -50,11 +53,31 @@ export default function RemoteControl() {
} }
}); });
socket.on('offer', async (data) => {
console.log('offer', data);
peerConnectionRef.current = new RTCPeerConnection({
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
});
await peerConnectionRef.current.setRemoteDescription(new RTCSessionDescription(data.offer));
const answer = await peerConnectionRef.current.createAnswer();
await peerConnectionRef.current.setLocalDescription(answer);
socket.emit('answer', {
targetId: data.targetId,
type: 'answer',
answer
});
})
socket.on('error', async ( data ) => { socket.on('error', async ( data ) => {
alert(`Error: ${data}`); alert(`Error: ${data}`);
setStatus('disconnected'); setStatus('disconnected');
}); });
socket.on('requestControl', async (data) => {
console.log(data)
})
return () => { return () => {
socket.disconnect(); socket.disconnect();
}; };
@@ -62,47 +85,50 @@ export default function RemoteControl() {
// ▶️ 연결 시작 // ▶️ 연결 시작
const startControl = async () => { const startControl = async () => {
socketRef.current.emit('requestControl', {
if (!employeeId) return; targetId: employeeId,
displayId: selectedDisplay
setStatus('connecting');
const pc = new RTCPeerConnection({
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
}); });
peerConnectionRef.current = pc; // if (!employeeId) return;
//
pc.ontrack = (event) => { // setStatus('connecting');
console.log(event) // const pc = new RTCPeerConnection({
if (videoRef.current) { // iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
videoRef.current.srcObject = event.streams[0]; // });
} // peerConnectionRef.current = pc;
}; //
// pc.ontrack = (event) => {
pc.onicecandidate = (event) => { // console.log(event)
if (event.candidate && socketRef.current) { // if (videoRef.current) {
socketRef.current.emit('webrtcSignal', { // videoRef.current.srcObject = event.streams[0];
targetId: employeeId, // }
type: 'icecandidate', // };
candidate: event.candidate //
}); // pc.onicecandidate = (event) => {
} // if (event.candidate && socketRef.current) {
}; // socketRef.current.emit('webrtcSignal', {
// targetId: employeeId,
try { // type: 'icecandidate',
const offer = await pc.createOffer({ offerToReceiveVideo: true }); // candidate: event.candidate
await pc.setLocalDescription(offer); // });
// }
// };
socketRef.current.emit('requestControl', { //
targetId: employeeId, // try {
offer: offer, // const offer = await pc.createOffer({ offerToReceiveVideo: true });
displayId: selectedDisplay // await pc.setLocalDescription(offer);
}); //
} catch (err) { //
setStatus('disconnected'); // socketRef.current.emit('requestControl', {
console.error('연결 실패:', err); // targetId: employeeId,
setStatus('disconnected'); // offer: offer,
} // displayId: selectedDisplay
// });
// } catch (err) {
// setStatus('disconnected');
// console.error('연결 실패:', err);
// setStatus('disconnected');
// }
}; };
// 🖱️ 입력 이벤트 전달 // 🖱️ 입력 이벤트 전달
@@ -165,7 +191,7 @@ export default function RemoteControl() {
/> />
</div> </div>
{displays.length > 0 && ( {displays != null && (
<div style={{ marginBottom: '16px' }}> <div style={{ marginBottom: '16px' }}>
<label style={{ fontSize: '18px', marginRight: '10px' }}>모니터 선택:</label> <label style={{ fontSize: '18px', marginRight: '10px' }}>모니터 선택:</label>
<select <select