-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
249 lines (211 loc) · 8.46 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// const WebSocket = require("ws");
// const express = require("express");
// const path = require("path");
// const http = require("http");
// const cors = require("cors");
// const app = express();
// app.use(cors({ origin: "*" }));
// const server = http.createServer(app);
// const wss = new WebSocket.Server({ server });
// app.use(express.static(path.join(__dirname, "."))); // Serve Frontend
// // Store users in rooms
// let users = {};
// let rooms = {}; // { roomName: [clients] }
// wss.on("connection", (ws) => {
// ws.on("message", (message) => {
// const data = JSON.parse(message);
// if (data.type === "join") {
// ws.room = data.room;
// ws.username = data.username;
// if (!users[data.room]) {
// users[data.room] = [];
// }
// users[data.room].push({ username: data.username, ws });
// // Notify all clients about updated user list
// broadcastUsersList(data.room);
// }
// if (data.type === "leave") {
// users[data.room] = users[data.room].filter(user => user.username !== data.username);
// broadcastUsersList(data.room);
// }
// });
// function broadcastUsersList(room) {
// const userList = users[room]?.map(user => user.username) || [];
// const userListMessage = JSON.stringify({ type: "user_list", users: userList, room });
// users[room]?.forEach(user => {
// if (user.ws.readyState === WebSocket.OPEN) {
// user.ws.send(userListMessage);
// }
// });
// }
// if (data.type === "message") {
// // Broadcast message to all users in the same room
// rooms[data.room]?.forEach(client => {
// if (client.readyState === WebSocket.OPEN) {
// client.send(JSON.stringify(data));
// }
// });
// } else if (data.type === "typing" || data.type === "stop_typing") {
// rooms[data.room]?.forEach(client => {
// if (client !== ws && client.readyState === WebSocket.OPEN) {
// client.send(JSON.stringify(data));
// } });
// }
// else if (data.type === "file") {
// if (rooms[data.room]) {
// rooms[data.room].forEach(client => {
// if (client.readyState === WebSocket.OPEN) {
// client.send(JSON.stringify(data));
// }
// });
// }
// }
// else if (data.type === "seen") {
// rooms[data.room]?.forEach(client => {
// if (client !== ws && client.readyState === WebSocket.OPEN) {
// client.send(JSON.stringify(data));
// } });
// }
// else if (data.type === "join") {
// ws.room = data.room;
// if (!rooms[data.room]) {
// rooms[data.room] = [];
// }
// rooms[data.room].push(ws);
// console.log(`${data.username} joined room: ${data.room}`);
// // Notify all users in the room
// const joinMsg = {
// type: "system",
// message: `${data.username} has joined the chat!`,
// room: data.room
// };
// rooms[data.room].forEach(client => {
// if (client.readyState === WebSocket.OPEN) {
// client.send(JSON.stringify(joinMsg));
// }
// });
// }
// });
// ws.on("close", () => {
// if (ws.room && ws.username) {
// users[ws.room] = users[ws.room].filter(user => user.username !== ws.username);
// broadcastUsersList(ws.room);
// }
// });
// server.listen(3000, '192.168.0.101', () => {
// console.log("Server running on all network interfaces");
// })
const WebSocket = require("ws");
const express = require("express");
const path = require("path");
const http = require("http");
const cors = require("cors");
const app = express();
app.use(cors({ origin: "*" }));
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
app.use(express.static(path.join(__dirname, "."))); // Serve Frontend
// Store users in rooms
let users = {};
let rooms = {}; // { roomName: [{ ws, username }] }
wss.on("connection", (ws) => {
ws.on("message", (message) => {
const data = JSON.parse(message);
if (data.type === "join") {
ws.room = data.room;
ws.username = data.username;
if (!users[data.room]) {
users[data.room] = [];
}
if (!rooms[data.room]) {
rooms[data.room] = [];
}
// ✅ Store user in `users` and `rooms`
users[data.room].push({ username: data.username, ws });
rooms[data.room].push({ username: data.username, ws });
console.log(`${data.username} joined room: ${data.room}`);
// ✅ Notify all users in the room
const joinMsg = {
type: "system",
message: `${data.username} has joined the chat!`,
room: data.room
};
broadcast(data.room, joinMsg);
// ✅ Broadcast updated user list
broadcastUsersList(data.room);
}
else if (data.type === "leave") {
// ✅ Notify all users in the room
const leaveMsg = {
type: "system",
message: `${data.username} has left the chat.`,
room: data.room
};
broadcast(data.room, leaveMsg);
// ✅ Remove user from room list
users[data.room] = users[data.room].filter(user => user.username !== data.username);
rooms[data.room] = rooms[data.room].filter(client => client.username !== data.username);
// ✅ Update user list for everyone
broadcastUsersList(data.room);
}
else if (data.type === "message") {
// ✅ Broadcast message to all users in the same room
broadcast(data.room, data);
}
else if (data.type === "typing" || data.type === "stop_typing") {
rooms[data.room]?.forEach(client => {
if (client.ws !== ws && client.ws.readyState === WebSocket.OPEN) {
client.ws.send(JSON.stringify(data));
}
});
}
else if (data.type === "file") {
broadcast(data.room, data);
}
else if (data.type === "seen") {
rooms[data.room]?.forEach(client => {
if (client.ws !== ws && client.ws.readyState === WebSocket.OPEN) {
client.ws.send(JSON.stringify(data));
}
});
}
});
// ✅ Function to Broadcast Messages in a Room
function broadcast(room, data) {
rooms[room]?.forEach(client => {
if (client.ws.readyState === WebSocket.OPEN) {
client.ws.send(JSON.stringify(data));
}
});
}
// ✅ Function to Broadcast Active Users List
function broadcastUsersList(room) {
const userList = users[room]?.map(user => user.username) || [];
const userListMessage = JSON.stringify({ type: "user_list", users: userList, room });
users[room]?.forEach(user => {
if (user.ws.readyState === WebSocket.OPEN) {
user.ws.send(userListMessage);
}
});
}
// ✅ Handle Disconnection
ws.on("close", () => {
if (ws.room && ws.username) {
// ✅ Send system message that user left
const disconnectMsg = {
type: "system",
message: `${ws.username} has left the chat.`,
room: ws.room
};
broadcast(ws.room, disconnectMsg);
// ✅ Remove user from lists
users[ws.room] = users[ws.room].filter(user => user.username !== ws.username);
rooms[ws.room] = rooms[ws.room].filter(client => client.username !== ws.username);
// ✅ Update user list for everyone
broadcastUsersList(ws.room);
}
});
});
server.listen(3000, '192.168.0.101', () => {
console.log("Server running on 192.168.0.101:3000");
});