-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (35 loc) · 1.13 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
require("dotenv").config();
const isDevelopment = process.env.NODE_ENV === "development";
const express = require("express");
const app = express();
const fs = require("fs");
let options = {};
if (isDevelopment) {
options = {
key: fs.readFileSync("./localhost.key"),
cert: fs.readFileSync("./localhost.crt"),
};
}
const server = require(isDevelopment ? "https" : "http").Server(options, app);
const port = process.env.PORT || 443;
app.use(express.static("public"));
server.listen(port, () => {
console.log(`App listening on port ${port}!`);
});
const { Server } = require("socket.io");
const io = new Server(server);
const clients = {};
io.on("connection", (socket) => {
clients[socket.id] = { id: socket.id };
socket.on("disconnect", () => {
io.emit("client-disconnect", clients[socket.id]);
delete clients[socket.id];
io.emit("clients", clients);
});
socket.on("signal", (peerId, signal) => {
console.log(`Received signal from ${socket.id} to ${peerId}`);
io.to(peerId).emit("signal", peerId, signal, socket.id);
});
io.emit("clients", clients);
io.emit("client-connection", clients[socket.id]);
});