-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (136 loc) · 3.91 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const path = require("path");
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const PORT = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, "public")));
app.get("/room/:roomId", (req, res) => {
const roomId = req.params.roomId;
if (/^\d{4}$/.test(roomId)) {
res.sendFile(path.join(__dirname, "public", "room.html"));
} else {
res.status(404).send("Invalid room ID. Please use a 4-digit number.");
}
});
app.get("*", (req, res) => {
res.redirect("/");
});
const rooms = new Map();
io.on("connection", (socket) => {
console.log("A user connected");
let currentRoom = null;
let currentUserName = null;
socket.on("join-room", ({ roomId, userName }) => {
currentRoom = roomId;
currentUserName = userName;
socket.join(roomId);
if (!rooms.has(roomId)) {
rooms.set(roomId, new Map());
}
rooms.get(roomId).set(socket.id, {
id: socket.id,
name: userName,
muted: true,
videoOff: true,
});
io.to(roomId).emit("user-connected", {
id: socket.id,
name: userName,
muted: true,
videoOff: true,
});
io.to(roomId).emit(
"update-user-list",
Array.from(rooms.get(roomId).values())
);
socket.on("disconnect", () => {
console.log("A user disconnected");
handleDisconnect();
});
socket.on("update-user-name", (newName) => {
if (currentRoom && rooms.has(currentRoom)) {
const user = rooms.get(currentRoom).get(socket.id);
if (user) {
user.name = newName;
currentUserName = newName;
io.to(currentRoom).emit(
"update-user-list",
Array.from(rooms.get(currentRoom).values())
);
}
}
});
socket.on("mute-status", (muted) => {
if (currentRoom && rooms.has(currentRoom)) {
const user = rooms.get(currentRoom).get(socket.id);
if (user) {
user.muted = muted;
io.to(currentRoom).emit(
"update-user-list",
Array.from(rooms.get(currentRoom).values())
);
}
}
});
socket.on("video-status", (videoOff) => {
if (currentRoom && rooms.has(currentRoom)) {
const user = rooms.get(currentRoom).get(socket.id);
if (user) {
user.videoOff = videoOff;
io.to(currentRoom).emit(
"update-user-list",
Array.from(rooms.get(currentRoom).values())
);
}
}
});
socket.on("heartbeat", (timestamp) => {
if (currentRoom) {
socket.to(currentRoom).emit("heartbeat", { senderId: socket.id, timestamp });
}
});
socket.on("heartbeat-ack", ({ targetId, roundTripTime }) => {
if (currentRoom) {
socket.to(targetId).emit("update-ping", { senderId: socket.id, pingTime: roundTripTime });
}
});
});
function handleDisconnect() {
if (currentRoom && rooms.has(currentRoom)) {
rooms.get(currentRoom).delete(socket.id);
if (rooms.get(currentRoom).size === 0) {
rooms.delete(currentRoom);
} else {
io.to(currentRoom).emit("user-disconnected", socket.id);
io.to(currentRoom).emit(
"update-user-list",
Array.from(rooms.get(currentRoom).values())
);
}
}
}
socket.on("offer", (data) => {
socket.to(data.target).emit("offer", {
sdp: data.sdp,
sender: socket.id,
});
});
socket.on("answer", (data) => {
socket.to(data.target).emit("answer", {
sdp: data.sdp,
sender: socket.id,
});
});
socket.on("ice-candidate", (data) => {
socket.to(data.target).emit("ice-candidate", {
candidate: data.candidate,
sender: socket.id,
});
});
});
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});