-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (46 loc) · 1.19 KB
/
server.js
File metadata and controls
55 lines (46 loc) · 1.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
const express = require('express');
const url = require('url');
const app = express();
const expressWs = require('express-ws')(app);
const aWss = expressWs.getWss('/');
const port = 3000;
const heartbeatInMilliseconds = 5000;
app.use(express.static('public'));
app.use(function (req, res, next) {
// console.log('middleware');
req.testing = 'testing';
return next();
});
// change any default route to a video file
app.get('/:video',function(req,res)
{
let v = {'video':'/video/' + req.params.video + '.mp4'};
res.redirect(url.format({
pathname:"/",
query:v
}));
res.end();
});
// socket routing so it doesn't get in the way of http requests
app.get('/socket', function(req, res, next){
console.log('get route', req.testing);
res.end();
});
app.ws('/socket', function(ws, req) {
ws.on('message', function(msg) {
// console.log(msg);
sendClientsMessage(msg);
});
});
app.listen(port);
// send out heartbeat to replay video
setInterval(function () {
sendClientsMessage('Play Video');
}, heartbeatInMilliseconds);
let sendClientsMessage = function(msg)
{
aWss.clients.forEach(function (client) {
client.send(msg);
});
}
console.log('Starting a lovely server!');