-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
401 lines (340 loc) · 10.2 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// server.js
const express = require('express');
const WebSocket = require('ws');
const http = require('http');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 8080;
// Middleware to parse JSON bodies for HTTP requests
app.use(express.json());
// Create an HTTP server
const server = http.createServer(app);
// Create WebSocket servers
const wss = new WebSocket.Server({ noServer: true }); // For audio streaming
const notificationWSS = new WebSocket.Server({ noServer: true }); // For notifications
// Store clients per channel and audio messages in memory
const clientsPerChannel = {};
const channelMessages = {};
// Store all connected notification clients
const notificationClients = new Map(); // Map of userId to WebSocket clients
// File path for storing channels
const CHANNELS_FILE = path.join(__dirname, 'channels.json');
// Load channels from file or initialize default channels
let channels = {};
if (fs.existsSync(CHANNELS_FILE)) {
try {
const data = fs.readFileSync(CHANNELS_FILE);
channels = JSON.parse(data);
} catch (e) {
console.error('Failed to load channels from file:', e);
channels = {};
}
}
// Ensure default channels exist
['Channel 1', 'Channel 2', 'Channel 3'].forEach((channelName) => {
if (!channels[channelName]) {
channels[channelName] = {
name: channelName,
creatorId: 'system',
isDefault: true,
members: null, // accessible to all
};
}
});
// Function to save channels to file
function saveChannelsToFile() {
try {
fs.writeFileSync(CHANNELS_FILE, JSON.stringify(channels, null, 2));
} catch (e) {
console.error('Failed to save channels to file:', e);
}
}
// Directory to save audio files
const AUDIO_DIRECTORY = path.join(__dirname, 'audio_files');
if (!fs.existsSync(AUDIO_DIRECTORY)) {
fs.mkdirSync(AUDIO_DIRECTORY);
}
// Function to save audio file
function saveAudioFile(channel, audioData) {
const fileName = `audio_${channel}_${Date.now()}.pcm`;
const filePath = path.join(AUDIO_DIRECTORY, fileName);
fs.writeFileSync(filePath, audioData);
return fileName;
}
// Handle WebSocket upgrades
server.on('upgrade', (request, socket, head) => {
const pathname = request.url;
if (pathname.startsWith('/ws/audio/')) {
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request);
});
} else if (pathname === '/ws/notifications') {
notificationWSS.handleUpgrade(request, socket, head, (ws) => {
notificationWSS.emit('connection', ws, request);
});
} else {
socket.destroy();
}
});
// WebSocket connection for live audio streaming
wss.on('connection', (ws, req) => {
const urlParts = req.url.split('/');
const channel = decodeURIComponent(urlParts[urlParts.length - 2]); // Extract channel from URL
const userId = decodeURIComponent(urlParts[urlParts.length - 1]); // Extract userId from URL
// Check if the user has access to the channel
const channelData = channels[channel];
if (!channelData) {
ws.send(JSON.stringify({ type: 'error', message: 'Channel not found' }));
ws.close();
return;
}
if (channelData.members && !channelData.members.includes(userId) && channelData.creatorId !== userId) {
ws.send(JSON.stringify({ type: 'error', message: 'Access denied to channel' }));
ws.close();
return;
}
if (!clientsPerChannel[channel]) {
clientsPerChannel[channel] = new Set();
}
clientsPerChannel[channel].add(ws);
console.log(`Client connected to channel: ${channel}`);
ws.on('message', (data) => {
if (clientsPerChannel[channel]) {
// Save audio data
saveAudioFile(channel, data);
// Broadcast audio to all clients in the channel except the sender
clientsPerChannel[channel].forEach((client) => {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
} else {
// Channel has been deleted; inform the client and close the connection
ws.send(JSON.stringify({ type: 'error', message: 'Channel has been deleted' }));
ws.close();
}
});
ws.on('close', () => {
if (clientsPerChannel[channel]) {
clientsPerChannel[channel].delete(ws);
if (clientsPerChannel[channel].size === 0) {
delete clientsPerChannel[channel];
}
}
console.log(`Client disconnected from channel: ${channel}`);
});
ws.on('error', (error) => {
console.error(`Error on channel ${channel}:`, error);
});
});
// WebSocket connection for notifications
notificationWSS.on('connection', (ws) => {
let userId = null;
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
if (data.type === 'register' && data.userId) {
userId = data.userId;
notificationClients.set(userId, ws);
console.log(`User ${userId} registered for notifications`);
}
} catch (e) {
console.error('Error parsing message:', e);
}
});
ws.on('close', () => {
if (userId) {
notificationClients.delete(userId);
}
console.log('Client disconnected from notification WebSocket');
});
ws.on('error', (error) => {
console.error('Error on notification WebSocket:', error);
});
});
// CRUD endpoints for channels
// Create a new channel
app.post('/channels', (req, res) => {
const { channelName, userId } = req.body;
if (!channelName || !userId) {
return res.status(400).json({
status: 'error',
message: 'Channel name and user ID are required',
});
}
if (channels[channelName]) {
return res.status(400).json({
status: 'error',
message: 'Channel already exists',
});
}
channels[channelName] = {
name: channelName,
creatorId: userId,
isDefault: false,
members: [userId],
};
saveChannelsToFile();
res.status(201).json({
status: 'success',
channelName,
});
});
// Get the list of channels
app.get('/channels', (req, res) => {
const userId = req.query.userId;
if (!userId) {
return res.status(400).json({
status: 'error',
message: 'User ID is required',
});
}
const accessibleChannels = Object.values(channels).filter(channel => {
if (channel.isDefault) return true;
if (channel.creatorId === userId) return true;
if (channel.members && channel.members.includes(userId)) return true;
return false;
});
res.status(200).json({
status: 'success',
channels: accessibleChannels,
});
});
// Delete a channel
app.delete('/channels/:channelName', (req, res) => {
const { channelName } = req.params;
const { userId } = req.body;
const channel = channels[channelName];
if (!channel) {
return res.status(404).json({
status: 'error',
message: 'Channel not found',
});
}
if (channel.creatorId !== userId) {
return res.status(403).json({
status: 'error',
message: 'Only the creator can delete this channel',
});
}
delete channels[channelName];
saveChannelsToFile();
// Notify and close any clients connected to this channel
if (clientsPerChannel[channelName]) {
clientsPerChannel[channelName].forEach((client) => {
client.send(JSON.stringify({
type: 'channel_deleted',
channelName: channelName,
}));
client.close(); // This will trigger the 'close' event on client side
});
delete clientsPerChannel[channelName];
}
// Remove messages associated with the channel
if (channelMessages[channelName]) {
delete channelMessages[channelName];
}
// Notify only the members of the channel about the deletion
const membersToNotify = new Set();
if (channel.members) {
channel.members.forEach((memberId) => {
membersToNotify.add(memberId);
});
}
// Also include the creator
membersToNotify.add(channel.creatorId);
membersToNotify.forEach((memberId) => {
const client = notificationClients.get(memberId);
if (client && client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
type: 'channel_deleted',
channelName: channelName,
}));
}
});
res.status(200).json({
status: 'success',
message: 'Channel deleted',
});
});
// Join a channel
app.post('/channels/:channelName/join', (req, res) => {
const { channelName } = req.params;
const { userId } = req.body;
if (!channelName || !userId) {
return res.status(400).json({
status: 'error',
message: 'Channel name and user ID are required',
});
}
const channel = channels[channelName];
if (!channel) {
return res.status(404).json({
status: 'error',
message: 'Channel not found',
});
}
if (channel.isDefault) {
return res.status(400).json({
status: 'error',
message: 'Cannot join default channels',
});
}
if (!channel.members) {
channel.members = [];
}
if (!channel.members.includes(userId)) {
channel.members.push(userId);
saveChannelsToFile();
}
res.status(200).json({
status: 'success',
message: `Joined channel ${channelName}`,
});
});
// Leave a channel
app.post('/channels/:channelName/leave', (req, res) => {
const { channelName } = req.params;
const { userId } = req.body;
if (!channelName || !userId) {
return res.status(400).json({
status: 'error',
message: 'Channel name and user ID are required',
});
}
const channel = channels[channelName];
if (!channel) {
return res.status(404).json({
status: 'error',
message: 'Channel not found',
});
}
if (channel.isDefault) {
return res.status(400).json({
status: 'error',
message: 'Cannot leave default channels',
});
}
if (channel.members && channel.members.includes(userId)) {
channel.members = channel.members.filter(id => id !== userId);
saveChannelsToFile();
res.status(200).json({
status: 'success',
message: `Left channel ${channelName}`,
});
} else {
res.status(400).json({
status: 'error',
message: 'User is not a member of the channel',
});
}
});
// Heartbeat endpoint to check server status
app.get('/heartbeat', (req, res) => {
res.status(200).json({ status: 'alive' });
});
// Start the server
server.listen(port, '0.0.0.0', () => {
console.log(`Server running and accessible from any IP on the network at port ${port}`);
});