-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (133 loc) · 5.03 KB
/
index.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
160
161
162
163
const express = require("express");
require('dotenv').config()
const os = require("os");
const fileUpload = require("express-fileupload");
const cors = require("cors");
const http = require("http");
const socketIo = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const membersList = [];
const userList = [];
const userSocketMap = {};
const port = 3300;
const { createProxyMiddleware } = require("http-proxy-middleware");
// Define a proxy route
const apiProxy = createProxyMiddleware("/api", {
target: "http://localhost:3300", // Replace with the URL of your target server
changeOrigin: true, // Necessary for virtual hosted sites
pathRewrite: {
"^/api": "", // Remove the '/api' prefix when forwarding the request
},
});
// Use the proxy middleware
app.use("/api", apiProxy);
// Use the cors middleware to allow all origins (for local development)
app.use(cors());
// Get the local IP address of the server
const localIpAddress = Object.values(os.networkInterfaces())
.flat()
.find((iface) => iface.family === "IPv4" && !iface.internal).address;
app.use(fileUpload());
app.use(express.static("./public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
// Serve files from the specified directory
app.use(
"/uploads",
express.static(process.env.server_Route)
);
io.on("connection", (socket) => {
console.log("A user connected");
socket.on("joinRoom", (data) => {
// Add the new user to the list of members
socket.userName = data.n;
socket.roomNo = data.r;
membersList.push(data.n);
// Add the user to the userList
userList.push({ name: data.n, room: data.r });
socket.join(data.r);
io.to(data.r).emit("jointoast", data.n);
io.to(data.r).emit("userJoined", data.n);
userSocketMap[data.n] = socket.id;
// Send the updated list of members to all clients in the room
io.to(data.r).emit(
"updateMembers",
membersList.filter((member) =>
userList.some((user) => user.room === data.r && user.name === member)
)
);
console.log(`User joined room: ${data.r}`);
});
app.post("/upload/:room", (req, res) => {
const room = req.params.room;
const sendList = req.body.sendlist.split(",");
const n = req.body.n;
console.log(req.body.n);
console.log(sendList);
const uploadedFiles = req.files['files[]']; // Access all uploaded files
if (!uploadedFiles || Object.keys(uploadedFiles).length === 0) {
return res.status(400).send("No files were uploaded.");
}
const uploadedFilePaths = []; // Create an array to store file paths
// Process each uploaded file
for (const fileKey in uploadedFiles) {
const uploadedFile = uploadedFiles[fileKey];
console.log(uploadedFile.name);
uploadedFile.mv(
`${process.env.server_Route}/${uploadedFile.name}`,
(err) => {
if (err) {
return res.status(500).send(err);
}
const uploadedFilePath = `/uploads/${uploadedFile.name}`;
uploadedFilePaths.push(uploadedFilePath); // Add file path to the array
// If all files are processed, emit the array of file paths to specific users
if (uploadedFilePaths.length === Object.keys(uploadedFiles).length) {
sendList.forEach((userName) => {
const socketId = userSocketMap[userName];
if (socketId) {
io.to(socketId).emit("filesUploaded", {
filePaths: uploadedFilePaths,
room,
});
io.to(socketId).emit("link", { filePath: uploadedFilePath, room });
console.log("n->",n);
io.to(socketId).emit("sender",n);
}
});
res.status(200).send(`Files uploaded to room: ${room}`);
}
}
);
}
});
// When a user disconnects
socket.on("disconnect", () => {
// Identify the userName and roomNo of the disconnected user
const userName = socket.userName; // Assuming you have set this during connection
const roomNo = socket.roomNo; // Assuming you have set this during connection
console.log(roomNo);
// Find and remove the disconnected user from the userList
const userIndex = userList.findIndex(
(user) => user.name === userName && user.room === roomNo
);
if (userIndex !== -1) {
userList.splice(userIndex, 1);
}
// Create a new list of members for the specific room
const roomMembers = userList.filter((user) => user.room === roomNo);
console.log(roomMembers);
// Extract only the 'name' attribute from the roomMembers list
const memberNames = roomMembers.map((user) => user.name);
delete userSocketMap[userName];
// Emit the updated member list (names only) to all users in the room
io.to(roomNo).emit("updateMembers", memberNames);
io.to(roomNo).emit("left", userName);
});
});
server.listen(port, () => {
console.log(`Server is running at http://${localIpAddress}:${port}`);
});