-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (57 loc) · 1.79 KB
/
server.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
const express = require("express");
const path = require("path");
const app = express();
const httpServer = require("http").createServer(app);
// const websocketServer = require("websocket").server;
// const io = new websocketServer({
// httpServer,
// });
const io = require("socket.io")(httpServer);
const public = path.join(__dirname, "/public");
app.use(express.static(public));
app.get("/", (req, res) => {
res.sendFile(path.join(public, "calling.html"));
});
app.get("/rtc/*", (req, res) => {
res.sendFile(path.join(public, "webrtc.html"));
});
app.get("/authrtc/*", (req, res) => {
res.sendFile(path.join(public, "webrtc.html"));
});
function logIt(msg) {
const date = new Date();
console.log(`${date}:${msg}`);
}
io.on("connection", function (socket) {
socket.on("join", function (room) {
logIt(`A client joined the room ${room}`);
const clients = io.sockets.adapter.rooms.get(room);
const numClients = typeof clients !== "undefined" ? clients.size : 0;
if (numClients === 0) {
socket.join(room);
} else if (numClients === 1) {
socket.join(room);
logIt(`room ${room} Broadcasting ready message`);
socket.to(room).emit("ready", room);
} else {
socket.emit("full", room);
}
});
// Relay candidate messages
socket.on("icecandidate", function (candidate, room) {
logIt(`${room} Received candidate. Broadcasting... ${candidate}`);
socket.to(room).emit("icecandidate", candidate);
});
// Relay offers
socket.on("offer", function (offer, room) {
socket.to(room).emit("offer", offer);
});
// Relay answers
socket.on("answer", function (answer, room) {
socket.to(room).emit("answer", answer);
});
});
const port = process.env.PORT || 8000;
httpServer.listen(port, () => {
console.log(`http://localhost:${port}`);
});