-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
65 lines (53 loc) · 1.93 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
const express = require("express");
const cors = require("cors");
require("dotenv").config();
require("./db/conn");
const userRouter = require("./routes/userRoutes");
const doctorRouter = require("./routes/doctorRoutes");
const appointRouter = require("./routes/appointRoutes");
const path = require("path");
const notificationRouter = require("./routes/notificationRouter");
const { Server } = require("socket.io");
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
app.use("/api/user", userRouter);
app.use("/api/doctor", doctorRouter);
app.use("/api/appointment", appointRouter);
app.use("/api/notification", notificationRouter);
app.use(express.static(path.join(__dirname, "./client/build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
const io = new Server(8000, {
cors: true,
});
const emailToSocketIdMap = new Map();
const socketidToEmailMap = new Map();
io.on("connection", (socket) => {
// console.log(`Socket Connected`, socket.id);
socket.on("room:join", (data) => {
const { email, room } = data;
emailToSocketIdMap.set(email, socket.id);
socketidToEmailMap.set(socket.id, email);
io.to(room).emit("user:joined", { email, id: socket.id });
socket.join(room);
io.to(socket.id).emit("room:join", data);
});
socket.on("user:call", ({ to, offer }) => {
io.to(to).emit("incomming:call", { from: socket.id, offer });
});
socket.on("call:accepted", ({ to, ans }) => {
io.to(to).emit("call:accepted", { from: socket.id, ans });
});
socket.on("peer:nego:needed", ({ to, offer }) => {
// console.log("peer:nego:needed", offer);
io.to(to).emit("peer:nego:needed", { from: socket.id, offer });
});
socket.on("peer:nego:done", ({ to, ans }) => {
// console.log("peer:nego:done", ans);
io.to(to).emit("peer:nego:final", { from: socket.id, ans });
});
});
app.listen(port, () => {});