-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (80 loc) · 2.68 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
const express = require("express");
const path = require("path");
const bodyparser = require("body-parser");
const app = express();
const dotenv = require("dotenv");
const connectDB = require("./Server/database/connection");
dotenv.config({ path: "config.env" });
const PORT = process.env.PORT || 8080;
connectDB();
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json());
app.set("view engine", "ejs");
app.use("/css", express.static(path.resolve(__dirname, "Assets/css")));
app.use("/img", express.static(path.resolve(__dirname, "Assets/img")));
app.use("/js", express.static(path.resolve(__dirname, "Assets/js")));
app.use("/", require("./Server/routes/router"));
var server = app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
const io = require("socket.io")(server, {
allowEIO3: true, //False by default
});
var userConnection = [];
io.on("connection", (socket) => {
console.log("Socket id is: ", socket.id);
socket.on("userconnect", (data) => {
console.log("Logged in username", data.displayName);
userConnection.push({
connectionId: socket.id,
user_id: data.displayName,
});
var userCount = userConnection.length;
console.log("UserCount", userCount);
});
socket.on("offerSentToRemote", (data) => {
var offerReceiver = userConnection.find(
(o) => o.user_id === data.remoteUser
);
if (offerReceiver) {
console.log("OfferReceiver user is: ", offerReceiver.connectionId);
socket.to(offerReceiver.connectionId).emit("ReceiveOffer", data);
}
});
socket.on("answerSentToUser1", (data) => {
var answerReceiver = userConnection.find(
(o) => o.user_id === data.receiver
);
if (answerReceiver) {
console.log("answerReceiver user is: ", answerReceiver.connectionId);
socket.to(answerReceiver.connectionId).emit("ReceiveAnswer", data);
}
});
socket.on("candidateSentToUser", (data) => {
var candidateReceiver = userConnection.find(
(o) => o.user_id === data.remoteUser
);
if (candidateReceiver) {
console.log(
"candidateReceiver user is: ",
candidateReceiver.connectionId
);
socket.to(candidateReceiver.connectionId).emit("candidateReceiver", data);
}
});
socket.on("disconnect", () => {
console.log("User disconnected");
var disUser = userConnection.find((p) => (p.connectionId = socket.id));
if (disUser) {
userConnection = userConnection.filter(
(p) => (p.connectionId = !socket.id)
);
console.log(
"Rest users username are: ",
userConnection.map(function (user) {
return user.user_id;
})
);
}
});
});