-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
66 lines (62 loc) · 1.76 KB
/
server.ts
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
import http from "node:http";
import fs from "node:fs/promises";
import { Server } from "socket.io";
const app = http
.createServer(async (req, res) => {
if (req.url?.endsWith("/index.html")) {
const file = await fs.readFile("./index.html");
return res.end(file);
}
if (req.url?.endsWith("/js/main.js")) {
const file = await fs.readFile("./js/main.js");
return res.end(file);
}
if (req.url?.endsWith("/css/main.css")) {
const file = await fs.readFile("./css/main.css");
return res.setHeader("Content-Type", "text/css").end(file);
}
const file = await fs.readFile("./index.html");
return res.end(file);
})
.listen(8080);
console.log("Listening on http://localhost:8080");
type OutboundMessage =
| RTCSessionDescriptionInit
| RTCIceCandidateInit
| CandidateMessage
| "peerIsReady"
| "bye";
type InboundMessage = {
room: string;
userId: string;
message: OutboundMessage;
};
type CandidateMessage = {
type: "candidate";
label: number;
id: string;
candidate: string;
};
const io = new Server(app);
io.sockets.on("connection", function (socket) {
socket.on("message", function (message: InboundMessage) {
console.log("Client said: ", message);
for (const room of socket.rooms) {
if (room == message.room) {
socket.to(room).emit("message", message);
return;
}
}
console.error(`Couldn't find room ${message.room}`);
});
socket.on("joinRoom", (room: string) => {
console.log("Client ID " + socket.id + " joined room " + room);
io.sockets.in(room).emit("join", room);
socket.join(room);
socket.emit("joined", room, socket.id);
io.sockets.in(room).emit("ready");
});
socket.on("bye", function () {
console.log("received bye");
});
});