forked from wetfish/sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (59 loc) · 2.29 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
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;
/*
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('/bulma.min.css',(req,res)=> res.sendFile(__dirname + '/node_modules/bulma/css/bulma.min.css'));
app.get('/fscreen.js', (req, res) => res.sendFile(__dirname + '/node_modules/fscreen/src/index.js'));
// Start server
let 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]}`;
let timestamp = mediaPlayer.getTimestamp();
let mediaType = mediaPlayer.mediaTypes[index];
let 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();
setInterval(() => {
let index = mediaPlayer.mediaIndex;
let total = mediaPlayer.playlist.length;
let timestamp = mediaPlayer.getTimestamp();
let mediaType = mediaPlayer.mediaTypes[index];
let data = {
humanReadableIndex: index + 1,
mediaType: mediaType,
timestamp: timestamp,
totalFiles: total
};
io.sockets.emit('timestamp', data);
}, 3000);