-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (84 loc) · 3.19 KB
/
Copy pathserver.js
File metadata and controls
94 lines (84 loc) · 3.19 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
const express = require('express');
const socket = require('socket.io');
require('dotenv').config();
const MediaPlayer = require('./media-player');
const app = express();
const playlistUrl = process.env.URL || 'http://localhost:3000';
const port = process.env.PORT || 3000;
const repeat = process.env.REPEAT;
const argv = require('yargs-parser')(process.argv);
/*
Actually... all of this stuff should be served by a real webserver, so uploading files to clients doesn't block the websocket thread
*/
// Basic Middleware
app.use(express.static(__dirname + '/public'));
// svg Sprites from Font Awesome
app.use(express.static(__dirname + '/node_modules/@fortawesome/fontawesome-free/sprites'));
// Basic Routes
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/views/index.html');
});
// Hosted frameworks and libraries
app.get('/vue.js', (req, res) => res.sendFile(__dirname + '/node_modules/vue/dist/vue.js'));
app.get('/socket.io.js', (req, res) => res.sendFile(__dirname + '/node_modules/socket.io-client/dist/socket.io.js'));
app.get('/fscreen.js', (req, res) => res.sendFile(__dirname + '/node_modules/fscreen/dist/fscreen.esm.js'));
// Start server
const server = app.listen(port, () => {
console.log(`Server listening on port: ${port}`);
});
// Socket setup
let io = socket(server);
io.on('connection', (client) => {
let index = mediaPlayer.mediaIndex;
let url = `${playlistUrl}${mediaPlayer.playlist[index]}`;
if (argv.m3u) {
//if the url is remote, don't append the project root url
if (url.startsWith('http')) {
url = `${mediaPlayer.playlist[index]['url']}`;
}
else {
url = `${playlistUrl}${mediaPlayer.playlist[index]['url']}`;
}
}
else url = `${playlistUrl}${mediaPlayer.playlist[index]}`;
const timestamp = mediaPlayer.getTimestamp();
const mediaType = mediaPlayer.mediaTypes[index];
const duration = mediaPlayer.mediaLengths[index];
console.log(`Client connected! Now playing ${mediaType} file ${url}. Timestamp: ${timestamp}`);
client.emit('updateClient', {
mediaType: mediaType,
timestamp: timestamp,
duration: duration,
url: url
});
});
// Start mediaPlayer
let mediaPlayer = new MediaPlayer(io);
mediaPlayer.init();
// Stop server depending on value given from REPEAT constant
function checkRepeat(repeat, count) {
if (repeat == count) {
console.log('we have played through the list '+count+' times');
process.exit('bye bye!');
}
else if (repeat==='false'&& count == 1) {
console.log('we have played through the list');
process.exit('bye bye!');
}
//if there's no repeat count or repeat is anything other than false, repeat ad infinitum
}
setInterval(() => {
let index = mediaPlayer.mediaIndex;
let total = mediaPlayer.playlist.length;
let timestamp = mediaPlayer.getTimestamp();
let mediaType = mediaPlayer.mediaTypes[index];
let playlistCount = mediaPlayer.playlistCount;
let data = {
humanReadableIndex: index + 1,
mediaType: mediaType,
timestamp: timestamp,
totalFiles: total
};
checkRepeat(repeat,playlistCount);
io.sockets.emit('timestamp', data);
}, 3000);