-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
741 lines (640 loc) Β· 23.8 KB
/
server.js
File metadata and controls
741 lines (640 loc) Β· 23.8 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
require('dotenv').config();
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const bodyParser = require('body-parser');
const path = require('path');
const fs = require('fs');
const NodeMediaServer = require('node-media-server');
const EventEmitter = require('events');
const PollManager = require('./server/pollManager');
const STREAM_KEY = process.env.STREAM_KEY || 'Testing';
const CHAT_HISTORY_FILE = path.join(__dirname, 'data', 'chat_history.json');
const MAX_CHAT_HISTORY = 100;
const HIGHLIGHTS_FILE = path.join(__dirname, 'data', 'highlights.json');
const MAX_HIGHLIGHTS = 6;
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
let isStreaming = false;
let viewerCount = 0;
let chatHistory = [];
let highlights = [];
// Load chat history from file
function loadChatHistory() {
try {
// Ensure data directory exists
const dir = path.dirname(CHAT_HISTORY_FILE);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
if (fs.existsSync(CHAT_HISTORY_FILE)) {
const data = fs.readFileSync(CHAT_HISTORY_FILE, 'utf8');
chatHistory = JSON.parse(data || '[]');
console.log(`Loaded ${chatHistory.length} messages from chat history`);
// Clean up old messages if exceeding max
if (chatHistory.length > MAX_CHAT_HISTORY) {
chatHistory = chatHistory.slice(-MAX_CHAT_HISTORY);
saveChatHistory(); // Save the cleaned up history
}
} else {
// Create the file if it doesn't exist
fs.writeFileSync(CHAT_HISTORY_FILE, '[]');
chatHistory = [];
console.log('Created new chat history file');
}
} catch (err) {
console.error('Error loading chat history:', err);
chatHistory = [];
}
}
// Save chat history to file
function saveChatHistory() {
try {
// Ensure data directory exists
const dir = path.dirname(CHAT_HISTORY_FILE);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(CHAT_HISTORY_FILE, JSON.stringify(chatHistory, null, 2));
console.log(`Saved ${chatHistory.length} messages to chat history`);
} catch (err) {
console.error('Error saving chat history:', err);
}
}
// Load highlights from file
function loadHighlights() {
try {
// Ensure data directory exists
const dir = path.dirname(HIGHLIGHTS_FILE);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
if (fs.existsSync(HIGHLIGHTS_FILE)) {
const data = fs.readFileSync(HIGHLIGHTS_FILE, 'utf8');
highlights = JSON.parse(data || '[]');
console.log(`Loaded ${highlights.length} highlights from file`);
// Clean up if exceeding max
if (highlights.length > MAX_HIGHLIGHTS) {
highlights = highlights.slice(-MAX_HIGHLIGHTS);
saveHighlights(); // Save the cleaned up highlights
}
} else {
// Create the file if it doesn't exist
fs.writeFileSync(HIGHLIGHTS_FILE, '[]');
highlights = [];
console.log('Created new highlights file');
}
} catch (err) {
console.error('Error loading highlights:', err);
highlights = [];
}
}
// Save highlights to file
function saveHighlights() {
try {
// Ensure data directory exists
const dir = path.dirname(HIGHLIGHTS_FILE);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
console.log(`Created directory: ${dir}`);
}
fs.writeFileSync(HIGHLIGHTS_FILE, JSON.stringify(highlights, null, 2));
console.log(`Saved ${highlights.length} highlights to file: ${HIGHLIGHTS_FILE}`);
} catch (err) {
console.error('Error saving highlights:', err);
}
}
// Add a highlight
function addHighlight(highlight) {
// Ensure we don't exceed the maximum
if (highlights.length >= MAX_HIGHLIGHTS) {
// Remove the oldest highlight
highlights.shift();
}
// Add the new highlight
highlights.push(highlight);
// Save to file
saveHighlights();
// Broadcast the updated highlights
broadcastHighlights();
}
// Remove a highlight
function removeHighlight(id) {
const index = highlights.findIndex(h => h.id === id);
if (index !== -1) {
highlights.splice(index, 1);
saveHighlights();
broadcastHighlights();
return true;
}
return false;
}
// Broadcast highlights to all connected clients
function broadcastHighlights() {
broadcast({
type: 'HIGHLIGHTS_UPDATE',
highlights: highlights
});
}
// Initialize
loadChatHistory();
loadHighlights();
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public'), {
setHeaders: (res, path) => {
if (path.endsWith('.js')) {
res.setHeader('Content-Type', 'application/javascript');
}
}
}));
// Proxy HLS requests to the media server
app.get('/live/:stream/:file', (req, res) => {
const stream = req.params.stream;
const file = req.params.file;
const hlsUrl = `http://localhost:8000/live/${stream}/${file}`;
console.log(`π‘ Proxying HLS request to: ${hlsUrl}`);
// Set appropriate headers for HLS content
if (file.endsWith('.m3u8')) {
res.setHeader('Content-Type', 'application/vnd.apple.mpegurl');
} else if (file.endsWith('.ts')) {
res.setHeader('Content-Type', 'video/mp2t');
}
// Improved proxy implementation with error handling
const proxyReq = http.get(hlsUrl, (proxyRes) => {
// Copy all headers from the proxied response
Object.keys(proxyRes.headers).forEach(key => {
res.setHeader(key, proxyRes.headers[key]);
});
// Set status code
res.status(proxyRes.statusCode);
// Pipe the response data
proxyRes.pipe(res);
// Log success
console.log(`β
Successfully proxied HLS request: ${file} (${proxyRes.statusCode})`);
});
proxyReq.on('error', (err) => {
console.error(`β Error proxying HLS request for ${file}:`, err);
if (!res.headersSent) {
res.status(502).send(`Error proxying HLS request: ${err.message}`);
}
});
// Handle client disconnect
req.on('close', () => {
proxyReq.destroy();
});
});
// Add a catch-all route for HLS segments that might have different patterns
app.get('/live/:stream/*', (req, res) => {
const stream = req.params.stream;
const pathParts = req.path.split('/');
const file = pathParts[pathParts.length - 1];
const hlsUrl = `http://localhost:8000${req.path}`;
console.log(`π‘ Proxying additional HLS request to: ${hlsUrl}`);
// Set appropriate headers based on file extension
if (file.endsWith('.m3u8')) {
res.setHeader('Content-Type', 'application/vnd.apple.mpegurl');
} else if (file.endsWith('.ts')) {
res.setHeader('Content-Type', 'video/mp2t');
}
// Proxy the request
const proxyReq = http.get(hlsUrl, (proxyRes) => {
Object.keys(proxyRes.headers).forEach(key => {
res.setHeader(key, proxyRes.headers[key]);
});
res.status(proxyRes.statusCode);
proxyRes.pipe(res);
});
proxyReq.on('error', (err) => {
console.error(`β Error proxying additional HLS request:`, err);
if (!res.headersSent) {
res.status(502).send(`Error proxying request: ${err.message}`);
}
});
req.on('close', () => {
proxyReq.destroy();
});
});
// Add this route to check if the HLS stream exists
app.get('/check-stream', (req, res) => {
const hlsUrl = `http://localhost:8000/live/StreamtoME/index.m3u8`;
console.log(`π Checking if HLS stream exists at: ${hlsUrl}`);
const checkReq = http.get(hlsUrl, (checkRes) => {
console.log(`β
HLS stream check result: ${checkRes.statusCode}`);
res.json({
exists: checkRes.statusCode === 200,
statusCode: checkRes.statusCode,
isStreaming: isStreaming
});
});
checkReq.on('error', (err) => {
console.error(`β Error checking HLS stream:`, err);
res.status(500).json({
exists: false,
error: err.message,
isStreaming: isStreaming
});
});
});
// Then your existing routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// This should be the LAST route
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.use((req, res) => {
console.log('404 - Not Found:', req.url);
res.status(404).send('Not Found');
});
// WebSocket connection handling
wss.on('connection', (ws) => {
console.log('New WebSocket client connected');
viewerCount++;
// Send initial stream status and viewer count
console.log('Sending initial stream status:', { isStreaming, viewerCount });
ws.send(JSON.stringify({
type: 'STREAM_STATUS',
status: isStreaming ? 'LIVE' : 'OFFLINE',
viewers: viewerCount
}));
// Send current poll if exists
const currentPoll = PollManager.getCurrentPoll();
if (currentPoll) {
console.log('Sending current poll to new client:', currentPoll);
ws.send(JSON.stringify({
type: 'POLL_UPDATE',
poll: currentPoll
}));
} else {
console.log('No active poll to send to new client');
}
// Send initial highlights
ws.send(JSON.stringify({
type: 'HIGHLIGHTS_UPDATE',
highlights: highlights
}));
// Ensure all chat messages have the proper format before sending
const recentMessages = chatHistory.slice(-50).map(msg => {
// If it's a string, convert it to a proper message object
if (typeof msg === 'string') {
return {
type: 'CHAT_MESSAGE',
platform: 'web',
username: 'Anonymous',
message: msg,
timestamp: new Date().toISOString(),
id: Date.now().toString()
};
}
// If it's already an object but missing fields, add defaults
if (typeof msg === 'object') {
return {
type: msg.type || 'CHAT_MESSAGE',
platform: msg.platform || 'web',
username: msg.username || 'Anonymous',
message: msg.message || '',
timestamp: msg.timestamp || new Date().toISOString(),
id: msg.id || Date.now().toString()
};
}
return msg;
});
console.log(`Sending ${recentMessages.length} recent chat messages to new client`);
ws.send(JSON.stringify({
type: 'CHAT_HISTORY',
messages: recentMessages
}));
// Broadcast updated viewer count
broadcast({
type: 'VIEWER_COUNT',
viewers: viewerCount
});
// Handle incoming messages
ws.on('message', async (message) => {
try {
const data = JSON.parse(message);
console.log('Received WebSocket message:', data);
switch(data.type) {
case 'REQUEST_STREAM_STATUS':
console.log('Sending stream status on request:', { isStreaming, viewerCount });
ws.send(JSON.stringify({
type: 'STREAM_STATUS',
status: isStreaming ? 'LIVE' : 'OFFLINE',
viewers: viewerCount
}));
break;
case 'CHAT_MESSAGE':
// Format the chat message
const chatMessage = {
type: 'CHAT_MESSAGE',
platform: data.platform || 'web',
username: data.username || 'Anonymous',
message: data.message,
timestamp: new Date().toISOString(),
id: Date.now().toString()
};
// Add to chat history
chatHistory.push(chatMessage);
console.log(`Added message to chat history. Total: ${chatHistory.length}`);
// Maintain maximum history size
if (chatHistory.length > MAX_CHAT_HISTORY) {
chatHistory.shift();
}
// Save chat history periodically (every 10 messages)
if (chatHistory.length % 10 === 0) {
saveChatHistory();
}
// Broadcast to all clients
broadcast(chatMessage);
break;
case 'REQUEST_CHAT_HISTORY':
console.log('Client requested chat history');
// Ensure all chat messages have the proper format before sending
const recentMessages = chatHistory.slice(-50).map(msg => {
// If it's a string, convert it to a proper message object
if (typeof msg === 'string') {
return {
type: 'CHAT_MESSAGE',
platform: 'web',
username: 'Anonymous',
message: msg,
timestamp: new Date().toISOString(),
id: Date.now().toString()
};
}
// If it's already an object but missing fields, add defaults
if (typeof msg === 'object') {
return {
type: msg.type || 'CHAT_MESSAGE',
platform: msg.platform || 'web',
username: msg.username || 'Anonymous',
message: msg.message || '',
timestamp: msg.timestamp || new Date().toISOString(),
id: msg.id || Date.now().toString()
};
}
return msg;
});
ws.send(JSON.stringify({
type: 'CHAT_HISTORY',
messages: recentMessages
}));
break;
case 'ADD_HIGHLIGHT':
if (data.highlight && data.isAdmin) {
// Generate a server-side ID if not provided
if (!data.highlight.id) {
data.highlight.id = 'highlight-' + Date.now();
}
// Add server timestamp
data.highlight.serverTimestamp = new Date().toISOString();
// Add the highlight
addHighlight(data.highlight);
// Confirm to the sender
ws.send(JSON.stringify({
type: 'HIGHLIGHT_ADDED',
highlight: data.highlight
}));
}
break;
case 'REMOVE_HIGHLIGHT':
if (data.id && data.isAdmin) {
const removed = removeHighlight(data.id);
// Confirm to the sender
ws.send(JSON.stringify({
type: 'HIGHLIGHT_REMOVED',
id: data.id,
success: removed
}));
}
break;
case 'REQUEST_HIGHLIGHTS':
// Send highlights to the requesting client
ws.send(JSON.stringify({
type: 'HIGHLIGHTS_UPDATE',
highlights: highlights
}));
break;
case 'CREATE_POLL':
console.log('Creating new poll:', data.poll);
if (data.poll) {
const newPoll = await PollManager.createPoll(data.poll);
// Broadcast the new poll to all clients
broadcast({
type: 'POLL_UPDATE',
poll: newPoll
});
}
break;
case 'SUBMIT_VOTE':
console.log('Processing vote:', data);
if (data.pollId && typeof data.optionIndex === 'number' && data.username) {
const updatedPoll = await PollManager.submitVote(data.pollId, data.optionIndex, data.username);
if (updatedPoll) {
broadcast({
type: 'POLL_UPDATE',
poll: updatedPoll
});
}
}
break;
case 'REQUEST_POLL_STATE':
const currentPoll = PollManager.getCurrentPoll();
ws.send(JSON.stringify({
type: 'POLL_UPDATE',
poll: currentPoll
}));
break;
case 'REQUEST_RECENT_POLL':
const recentPoll = PollManager.getMostRecentPoll();
if (recentPoll) {
ws.send(JSON.stringify({
type: 'POLL_UPDATE',
poll: recentPoll,
isActive: false
}));
}
break;
}
} catch (err) {
console.error('Error processing message:', err);
}
});
// Handle disconnection
ws.on('close', () => {
console.log('WebSocket client disconnected');
viewerCount = Math.max(0, viewerCount - 1);
// Broadcast updated viewer count
broadcast({
type: 'VIEWER_COUNT',
viewers: viewerCount
});
});
});
// Helper function to broadcast to all connected clients
function broadcast(message) {
console.log('Broadcasting message:', message);
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(message));
}
});
}
// Make broadcast function globally available for other modules
global.broadcast = broadcast;
// Save chat history more frequently (every minute)
setInterval(saveChatHistory, 60 * 1000);
app.post('/authenticate', (req, res) => {
const { name } = req.body;
if (name === STREAM_KEY) {
isStreaming = true;
broadcast({
type: 'STREAM_STATUS',
status: 'LIVE',
viewers: viewerCount
});
res.status(200).send('OK');
} else {
res.status(403).send('Forbidden');
}
});
app.post('/stream-ended', (req, res) => {
isStreaming = false;
broadcast({
type: 'STREAM_STATUS',
status: 'OFFLINE',
viewers: viewerCount
});
res.status(200).send('Stream Ended');
});
// Ensure chat history is saved on exit
process.on('SIGINT', () => {
console.log('Saving chat history before exit...');
saveChatHistory();
process.exit();
});
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
saveChatHistory();
});
// Update the config to remove HTTPS
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60
},
http: {
port: 8000,
allow_origin: '*',
mediaroot: './media'
},
trans: {
ffmpeg: '/usr/bin/ffmpeg', // Explicit path to ffmpeg on Ubuntu
tasks: [
{
app: 'live',
hls: true,
hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]',
hlsKeep: false,
dash: false,
}
]
}
};
// Create RTMP server instance
const nms = new NodeMediaServer(config);
// Extend nms with EventEmitter if needed (fallback)
if (typeof nms.on !== 'function') {
Object.setPrototypeOf(nms, EventEmitter.prototype);
EventEmitter.call(nms);
}
//console.log('NMS instance before run:', nms);
nms.run()
console.log('NMS started (HTTP and RTMP servers running)');
// Add more detailed logging for RTMP events
nms.on('preConnect', (id, args) => {
console.log('π [RTMP] Client attempting to connect:', id);
});
nms.on('postConnect', (id, args) => {
console.log('β
[RTMP] Client connected:', id);
});
nms.on('prePublish', (id, StreamPath, args) => {
console.log('π₯ [RTMP] Stream starting:', {
id: id,
path: StreamPath,
args: args
});
let stream_key = StreamPath.split('/')[2];
if (stream_key === STREAM_KEY) {
console.log('β
[RTMP] Stream key validated');
console.log('π‘ [HLS] HLS stream should be available at:', `http://localhost:8000/live/${stream_key}/index.m3u8`);
isStreaming = true;
broadcast({
type: 'STREAM_STATUS',
status: 'LIVE',
viewers: viewerCount
});
return;
}
throw new Error('Invalid stream key');
});
nms.on('donePublish', (id, StreamPath, args) => {
console.log('π [RTMP] Stream ended:', {
id: id,
path: StreamPath
});
isStreaming = false;
broadcast({
type: 'STREAM_STATUS',
status: 'OFFLINE',
viewers: viewerCount
});
});
// Add logging for stream chunks being generated
nms.on('postHLSSegment', (id, level, sn, duration, start, end) => {
console.log('πΌ [HLS] New segment generated:', {
id,
level,
segmentNumber: sn,
duration,
start,
end,
path: `live/StreamtoME/${sn}.ts`
});
});
// Add this after nms.run()
console.log('π Media root directory:', path.resolve(config.http.mediaroot));
console.log('π Expected HLS path:', path.resolve(config.http.mediaroot, 'live', STREAM_KEY));
// Check if the directory exists
const hlsDir = path.resolve(config.http.mediaroot, 'live', STREAM_KEY);
fs.access(hlsDir, fs.constants.F_OK, (err) => {
if (err) {
console.log('β οΈ HLS directory does not exist yet:', hlsDir);
// Create the directory structure
fs.mkdirSync(hlsDir, { recursive: true });
console.log('β
Created HLS directory:', hlsDir);
} else {
console.log('β
HLS directory exists:', hlsDir);
}
});
// Initialize PollManager when server starts
PollManager.loadPolls();
server.listen(3001, () => {
const isProduction = process.env.NODE_ENV === 'production';
const host = isProduction ? 'watch.stream150.com' : 'localhost';
const protocol = isProduction ? 'https' : 'http';
console.log('Backend server running on:');
console.log(`- Web: ${protocol}://${host}:3001`);
console.log(`- RTMP: rtmp://${host}:1935/live`);
console.log(`- HLS: ${protocol}://${host}:${isProduction ? '8443' : '8000'}/live`);
});