-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (142 loc) · 4.59 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const routes = require("./routes/routes");
const cors = require("cors");
const cron = require("node-cron");
const io = require("socket.io")(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});
dotenv.config();
mongoose.connect(process.env.DATABASE_CONNECTION, () =>
console.log("DB connection established")
);
app.use(cors());
app.use(express.static("userProfileImages"));
const PORT = process.env.PORT || 5000;
let accs = [];
io.on("connection", (socket) => {
let connections = Array.from(io.sockets.sockets).map((socket) => socket[0]);
socket.emit("me", socket.id, connections);
socket.on("disconnect", (soc) => {
for (let i = 0; i < accs.length; i++) {
if (accs[i]["clientId"] === socket.id) {
accs.splice(i, 1);
}
}
socket.broadcast.emit("callEnded");
});
socket.on("update", (account) => {
for (let i = 0; i < accs.length; i++) {
if (accs[i]["userName"] === account.userName) {
accs.splice(i, 1);
}
}
accs.push(account);
});
socket.on("callUser", (data) => {
for (let i = 0; i < accs.length; i++) {
if (accs[i]["clientId"] === data.from) {
accs.splice(i, 1);
}
}
io.to(data.userToCall).emit("callUser", {
signal: data.signalData,
from: data.from,
});
});
socket.on("buttonAction", (caller) => {
io.to(caller).emit("buttonAction");
});
socket.on("answerCall", (data) => {
io.to(data.to).emit("callAccepted", data.signal);
});
});
app.use(express.json());
app.use(cors());
app.use("/", routes);
server.listen(PORT, () => console.log(`server is running on port ${PORT}`));
// scheduled to run every 15 seconds
cron.schedule("*/15 * * * * *", () => {
if (accs.length > 1) {
const matchedUsersUnique = new Set();
// constantly try to match users
const matchedSenders = [];
const matchedRecievers = [];
const matchedRecieverIds = [];
for (let i = 0; i < accs.length; i++) {
for (let x = 0; x < accs.length; x++) {
if (
accs[i]["userName"] !== accs[x]["userName"] &&
accs[i]["nativeLang"] === accs[x]["targetLang"]
) {
let iFilterCount = 0;
let xFilterCount = 0;
let totalFilters = 0; // filters that are both true in i and y
let curr_i = accs[i]["filters"];
let curr_x = accs[x]["filters"];
for (let key in curr_i) {
if (curr_i[key] === true) {
iFilterCount++;
}
if (curr_x[key] === true) {
xFilterCount++;
}
if (curr_i[key] === true && curr_x[key] === true) {
totalFilters++;
}
}
// if account i and x have more 0 matching filters
if (
totalFilters > 0 &&
!matchedSenders.includes(accs[i]["clientId"]) &&
!matchedRecievers.includes(accs[x]["userName"]) &&
!matchedRecieverIds.includes(accs[x]["clientId"])
) {
matchedUsersUnique.add({
sender: accs[i]["clientId"],
reciever: accs[x]["userName"],
recieverId: accs[x]["clientId"],
});
matchedSenders.push(accs[i]["clientId"]);
matchedRecievers.push(accs[x]["userName"]);
matchedRecieverIds.push(accs[x]["clientId"]);
} else if (
// if accounts i or x have 0 filters selected
totalFilters === 0 &&
iFilterCount === 0 &&
xFilterCount === 0 &&
!matchedSenders.includes(accs[i]["clientId"]) &&
!matchedRecievers.includes(accs[x]["userName"]) &&
!matchedRecieverIds.includes(accs[x]["clientId"])
) {
matchedUsersUnique.add({
sender: accs[i]["clientId"],
reciever: accs[x]["userName"],
recieverId: accs[x]["clientId"],
});
matchedSenders.push(accs[i]["clientId"]);
matchedRecievers.push(accs[x]["userName"]);
matchedRecieverIds.push(accs[x]["clientId"]);
}
}
}
}
matchedUsers = Array.from(matchedUsersUnique);
if (matchedUsers.length > 1) {
for (let i = 0; i < matchedUsers.length; i++) {
io.to(matchedUsers[i]["sender"]).emit(
"matchedUser",
matchedUsers[i]["reciever"],
matchedUsers[i]["recieverId"]
);
}
}
}
});