-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
134 lines (107 loc) · 3.19 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require("dotenv").config();
const express = require("express");
const app = express();
const DbConnect = require("./database");
const router = require("./routes");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const ACTIONS = require("./actions");
const server = require("http").createServer(app);
const io = require("socket.io")(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});
app.use(cookieParser());
const corsOption = {
credentials: true,
origin: ["http://localhost:3000"],
};
app.use(cors(corsOption));
app.use("/storage", express.static("storage"));
const PORT = process.env.PORT || 5500;
DbConnect();
app.use(express.json({ limit: "8mb" }));
app.use(router);
app.get("/", (req, res) => {
res.send("Hello from express Js");
});
// Sockets
const socketUserMapping = {};
io.on("connection", (socket) => {
console.log("new connection", socket.id);
socket.on(ACTIONS.JOIN, ({ roomId, user }) => {
socketUserMapping[socket.id] = user;
// new Map
const clients = Array.from(io.sockets.adapter.rooms.get(roomId) || []);
clients.forEach((clientId) => {
io.to(clientId).emit(ACTIONS.ADD_PEER, {
peerId: socket.id,
createOffer: false,
user,
});
socket.emit(ACTIONS.ADD_PEER, {
peerId: clientId,
createOffer: true,
user: socketUserMapping[clientId],
});
});
socket.join(roomId);
});
// Handle relay Ice
socket.on(ACTIONS.RELAY_ICE, ({ peerId, icecandidate }) => {
io.to(peerId).emit(ACTIONS.ICE_CANDIDATE, {
peerId: socket.id,
icecandidate,
});
});
// Handle relay sdp ( session description )
socket.on(ACTIONS.RELAY_SDP, ({ peerId, sessionDescription }) => {
io.to(peerId).emit(ACTIONS.SESSION_DESCRIPTION, {
peerId: socket.id,
sessionDescription,
});
});
// Handle mute/unmute
socket.on(ACTIONS.MUTE, ({ roomid, userId }) => {
const clients = Array.from(io.sockets.adapter.rooms.get(roomid) || []);
clients.forEach((clientId) => {
io.to(clientId).emit(ACTIONS.MUTE, {
peerId: socket.id,
userId,
});
});
});
socket.on(ACTIONS.UN_MUTE, ({ roomId, userId }) => {
const clients = Array.from(io.sockets.adapter.rooms.get(roomId) || []);
clients.forEach((clientId) => {
io.to(clientId).emit(ACTIONS.UN_MUTE, {
peerId: socket.id,
userId,
});
});
});
// Leaving the room
const leaveRoom = ({ roomId }) => {
const { rooms } = socket;
Array.from(rooms).forEach((roomId) => {
const clients = Array.from(io.sockets.adapter.rooms.get(roomId) || []);
clients.forEach((clientId) => {
io.to(clientId).emit(ACTIONS.REMOVE_PEER, {
peerId: socket.id,
userId: socketUserMapping[socket.id]?.id,
});
socket.emit(ACTIONS.REMOVE_PEER, {
peerId: clientId,
userId: socketUserMapping[clientId]?.id,
});
});
socket.leave(roomId);
});
delete socketUserMapping[socket.id];
};
socket.on(ACTIONS.LEAVE, leaveRoom);
socket.on("disconnecting", leaveRoom);
});
server.listen(PORT, () => console.log(`Listening on port ${PORT}`));