-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js.save
More file actions
145 lines (120 loc) · 3.68 KB
/
Copy pathserver.js.save
File metadata and controls
145 lines (120 loc) · 3.68 KB
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
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const session = require("express-session");
const passport = require("passport");
const PORT = process.env.PORT || 4000;
const socket = require("socket.io");
const bodyParser = require("body-parser");
// database connection
require("./database/connection")();
// CORS policy
app.use(
cors({
origin:
[
"http://localhost:3000",
"http://localhost:5173",
"http://3.110.90.35:3000",
"http://3.109.78.94:3000",
"http://frontend.ayatrio.com",
"https://www.ayatrio.com",
"http://ayatrio-admin.s3-website.ap-south-1.amazonaws.com",
"https://main.d2e7lk624os6uh.amplifyapp.com",
],
methods: "GET,POST,PUT,DELETE,PATCH",
credentials: true,
})
);
app.use(express.json());
app.use(express.text());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
// setup session
app.use(
session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: true,
})
);
// // setuppassport
app.use(passport.initialize());
app.use(passport.session());
// // passport strategy for Google login
require("./config/passport")(passport);
// passport auth routes
app.use("/auth", require("./routes/googleAuth"));
// other routes
app.use("/api", require("./routes/routes"));
// home page routes
app.use("/api", require("./routes/homepageRoutes"));
// admin routes
app.use("/admin", require("./routes/admin"));
// admin routes
app.use("/indexing", require("./routes/indexing"));
// payment routes
app.use("/payment", require("./routes/paymentRoutes"));
// socket connection
const server = app.listen(PORT, () => {
console.log(`server started on http://localhost:${PORT}`);
});
const io = socket(server, {
cors: {
origin: ["http://localhost:3000", "http://frontend.ayatrio.com", "http://3.109.78.94:3000", "http://3.110.90.35:3000", "https://www.ayatrio.com", "http://localhost:5173"],
},
});
const roomToUsers = new Map();
io.on("connection", (socket) => {
socket.on("join-room", ({ roomId }) => {
if (!roomToUsers.has(roomId)) {
roomToUsers.set(roomId, new Set());
}
roomToUsers.get(roomId).add(socket.id);
socket.join(roomId);
io.to(roomId).emit("user-joined", {
userId: socket.id,
users: Array.from(roomToUsers.get(roomId)),
});
});
socket.on("leave-room", ({ roomId }) => {
if (roomToUsers.has(roomId)) {
roomToUsers.get(roomId).delete(socket.id);
io.to(roomId).emit("user-left", { userId: socket.id });
}
socket.leave(roomId);
});
socket.on("disconnect", () => {
roomToUsers.forEach((users, roomId) => {
if (users.has(socket.id)) {
users.delete(socket.id);
io.to(roomId).emit("user-left", { userId: socket.id });
}
});
});
socket.on("offer", ({ to, offer }) => {
io.to(to).emit("offer", { from: socket.id, offer });
});
socket.on("answer", ({ to, answer }) => {
io.to(to).emit("answer", { from: socket.id, answer });
});
socket.on("ice-candidate", ({ to, candidate }) => {
io.to(to).emit("ice-candidate", { from: socket.id, candidate });
});
socket.on("request_join", (data) => {
console.log("Join request received:", data);
io.emit("join_request", { socketId: socket.id, ...data });
});
socket.on("admin_response", (response) => {
console.log("Admin response received:", response);
const { socketId, accepted, roomId } = response;
if (accepted) {
io.to(socketId).emit("join_accepted", { roomId });
socket.join(roomId);
io.to(roomId).emit("join_accepted_admin", { roomId });
} else {
io.to(socketId).emit("join_rejected");
}
});
});