-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (80 loc) · 2.08 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
require("dotenv").config();
const express = require("express");
const http = require("http");
const app = express();
const socket = require("socket.io");
const userRoutes = require("./routes/users");
// creating a http server for peer and express
const server = http.Server(app);
const { ExpressPeerServer } = require("peer");
const cookieParser = require("cookie-parser");
const {
newMeeting,
joinMeetbyId,
leaveMeeting,
sendMessage,
} = require("./controllers/socketio");
const port = process.env.PORT || 8000;
//db conection
require("./db/conn");
// socket io config
const io = socket(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
//express server
const expServer = server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
// peer server
const peerServer = ExpressPeerServer(expServer, {
path: "/peer",
});
//middle wares
app.use(express.json());
app.use(cookieParser());
app.use(express.urlencoded({ extended: false }));
app.use(peerServer);
//routes
app.use(userRoutes);
// socket io events.
io.on("connection", (socket) => {
socket.on("newMeeting", () => {
return newMeeting(socket);
});
socket.on("join room", ({ roomID, user }) => {
joinMeetbyId(socket, roomID, user);
socket.on("disconnect", () => {
console.log("disconnect");
leaveMeeting(socket, roomID, user);
});
// chat event
socket.on("sendMessage", (data) => {
sendMessage(socket, data.message, data.user, roomID);
});
});
socket.on("sending signal", (payload) => {
console.log("sending");
io.to(payload.userToSignal).emit("user joined", {
signal: payload.signal,
callerID: payload.callerID,
user: payload.user
});
});
socket.on("returning signal", (payload) => {
console.log("receiving");
io.to(payload.callerID).emit("receiving returned signal", {
signal: payload.signal,
id: socket.id,
});
});
});
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}