mirror of
https://git.hmsn.ink/rtc-remote/server.git
synced 2026-03-19 16:04:58 +09:00
66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
// server.js
|
|
const express = require('express');
|
|
const http = require('http');
|
|
const socketIo = require('socket.io');
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const io = socketIo(server, {
|
|
cors: {
|
|
origin: "*", // 프로덕션에서는 정확한 도메인/포트 지정
|
|
methods: ["GET", "POST"]
|
|
}
|
|
});
|
|
|
|
// 직원 ID ↔ socket.id 매핑
|
|
const employeeSockets = new Map(); // { employeeId: socketId }
|
|
|
|
io.on('connection', (socket) => {
|
|
console.log('새 클라이언트 연결됨:', socket.id);
|
|
|
|
// 1. 직원이 자신의 ID를 등록 (예: "EMP123")
|
|
socket.on('register', (employeeId) => {
|
|
if (employeeId) {
|
|
employeeSockets.set(employeeId, socket.id);
|
|
socket.employeeId = employeeId;
|
|
console.log(`직원 등록됨: ${employeeId} → ${socket.id}`);
|
|
// 선택: 관리자에게 직원 온라인 알림
|
|
socket.broadcast.emit('employeeOnline', employeeId);
|
|
}
|
|
});
|
|
|
|
// 2. 관리자가 특정 직원에게 연결 요청
|
|
socket.on('requestControl', ({ targetId, offer }) => {
|
|
const targetSocketId = employeeSockets.get(targetId);
|
|
if (targetSocketId) {
|
|
io.to(targetSocketId).emit('controlRequest', {
|
|
from: socket.id,
|
|
offer
|
|
});
|
|
} else {
|
|
socket.emit('error', `직원 ${targetId}가 오프라인입니다.`);
|
|
}
|
|
});
|
|
|
|
// 3. WebRTC 시그널링 (SDP, ICE)
|
|
socket.on('webrtcSignal', ({ targetId, data }) => {
|
|
const targetSocketId = employeeSockets.get(targetId);
|
|
if (targetSocketId) {
|
|
io.to(targetSocketId).emit('webrtcSignal', { from: socket.id, data });
|
|
}
|
|
});
|
|
|
|
// 연결 종료 시 정리
|
|
socket.on('disconnect', () => {
|
|
if (socket.employeeId) {
|
|
employeeSockets.delete(socket.employeeId);
|
|
socket.broadcast.emit('employeeOffline', socket.employeeId);
|
|
console.log(`직원 오프라인: ${socket.employeeId}`);
|
|
}
|
|
});
|
|
});
|
|
|
|
const PORT = process.env.PORT || 3001;
|
|
server.listen(PORT, () => {
|
|
console.log(`시그널링 서버 실행 중: http://localhost:${PORT}`);
|
|
}); |