-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
68 lines (56 loc) · 1.87 KB
/
Copy pathserver.mjs
File metadata and controls
68 lines (56 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import express from "express";
import path from "path";
import { createServer } from "http";
import { Server } from "socket.io";
import { fileURLToPath } from "url";
const app = express();
const server = createServer(app);
const io = new Server(server);
let iceOfCaller = [];
let offer = null;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// public 폴더 정적 제공
app.use(express.static(path.join(__dirname, "public")));
io.on("connection", (socket) => {
console.log("WebSocket 연결됨:", socket.id);
// Caller → Server → Remote 로 Offer 전달
socket.on("offer", (sentoffer) => {
console.log("Offer 수신, 브로드캐스트");
offer = sentoffer;
});
socket.on("getOffer", () => {
if (!offer) {
console.log("오퍼없음");
} else {
socket.emit("sendOffer", offer);
}
});
// Remote → Server → Caller 로 Answer 전달
socket.on("answer", (answer) => {
console.log("Answer 수신, 브로드캐스트");
socket.broadcast.emit("sendAnswer", answer);
});
// 양쪽 ICE 후보 교환 (그냥 상대에게 브로드캐스트)
socket.on("ice-candidate", (candidate, isCaller) => {
console.log("ICE Candidate 수신, 브로드캐스트");
if (isCaller) {
iceOfCaller.push(candidate);
}
socket.broadcast.emit("send-ice-candidate", candidate);
});
socket.on("getIceOfCaller", () => {
while (iceOfCaller.length > 0) {
const c = iceOfCaller.shift();
try {
socket.emit("sendIceOfCaller", c);
console.log("대기중인 ICE 후보 추가 처리");
} catch (e) {
console.error("대기중인 ICE 후보 추가 중 에러:", e);
}
}
});
});
server.listen(3000, "0.0.0.0", () => {
console.log("서버 실행중: http://localhost:3000");
});