-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (51 loc) · 1.53 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
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3030;
const server = require("http").Server(app);
const { v4: uuidv4 } = require("uuid");
const io = require("socket.io")(server);
const { ExpressPeerServer } = require("peer");
const url = require("url");
const peerServer = ExpressPeerServer(server, {
debug: true,
});
const path = require("path");
app.set("view engine", "ejs");
app.use("/public", express.static(path.join(__dirname, "static")));
app.use("/peerjs", peerServer);
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "static", "index.html"));
});
app.get("/join", (req, res) => {
res.redirect(
url.format({
pathname: `/join/${uuidv4()}`,
query: req.query,
})
);
});
app.get("/joinold", (req, res) => {
res.redirect(
url.format({
pathname: req.query.meeting_id,
query: req.query,
})
);
});
app.get("/join/:rooms", (req, res) => {
res.render("room", { roomid: req.params.rooms, Myname: req.query.name });
});
io.on("connection", (socket) => {
socket.on("join-room", (roomId, id, myname) => {
socket.join(roomId);
socket.to(roomId).emit("user-connected", id, myname);
socket.on("tellName", (myname) => {
console.log(myname);
socket.to(roomId).emit("AddName", myname);
});
socket.on("disconnect", () => {
socket.to(roomId).emit("user-disconnected", id);
});
});
});
server.listen(PORT);