-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcaster.js
119 lines (101 loc) · 2.97 KB
/
broadcaster.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
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.raw({ inflate: true, limit: '100mb', type: 'image/jpeg' }));
const PORT = 18082;
let latitude = null;
let longitude = null;
let numSatelites = null;
let seaLevel = null;
let geoid = null;
let hmsJst = null;
function sendResp(res, err, doc) {
if (err) {
res.status(doc.status).send(doc.reason);
} else {
if (doc) {
res.send(doc);
} else {
res.send();
}
}
}
let watchers = {};
const BOUNDARY = "broadcaster";
app.get('/broadcast/:serivceId', (req, res) => {
let serivceId = req.params.serivceId;
res.on('close', () => {
if (serivceId in watchers) {
console.log("A watcher of " + serivceId.toString() + " is closed");
watchers[serivceId] = watchers[serivceId].filter ( v => res !== v );
if (watchers[serivceId].length == 0) {
delete watchers.serivceId;
console.log(serivceId.toString() + ' has no watchers');
}
}
});
if (serivceId in watchers) {
watchers[serivceId].push(res);
} else {
watchers[serivceId] = [res]
}
console.log(serivceId.toString() + ' connected');
res.writeHead(200, {
'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
'Connection': 'keep-alive',
// here we define the boundary marker for browser to identify subsequent frames
'Content-Type': `multipart/x-mixed-replace;boundary="${BOUNDARY}"`,
'Expires': 'Thu, Jan 01 1970 00:00:00 GMT',
'Pragma': 'no-cache'
});
});
app.post('/broadcast/:serivceId', (req, res) => {
let serivceId = req.params.serivceId;
sendResp(res, false, null);
// Broadcast the JPEG image to all the watchers
let buf = req.body;
if (serivceId in watchers) {
let l = watchers[serivceId];
l.forEach( r => {
r.write(`--${BOUNDARY}\r\n`);
r.write('Content-Type: image/jpeg\r\n');
r.write(`Content-Length: ${buf.length}\r\n`);
r.write('\r\n');
r.write(buf, 'binary');
r.write('\r\n');
});
}
});
app.post('/sensor/:serivceId/gps', (req, res) => {
let serivceId = req.params.serivceId;
latitude = req.query.latitude;
longitude = req.query.longitude;
numSatelites = req.query.numSatelites;
seaLevel = req.query.sealevel;
geoid = req.query.geoid;
hmsJst = req.query.hmsJst;
console.log(req.query);
sendResp(res, false, null);
});
app.get('/sensor/gps/:serviceId', (req, res) => {
if (latitude != null) {
res.json({
latitude: latitude,
longitude: longitude,
numSatelites: numSatelites,
seaLevel: seaLevel,
geoid: geoid,
hmsJst: hmsJst});
} else {
sendResp(res, true, null);
}
});
app.get('/map/:serviceId', (req, res) => {
res.sendFile(path.join(__dirname + '/webapp/map.html'));
});
// Start this broadcaster
const server = app.listen(PORT, () => {
console.log('Motion JPEG Broadcaster has started at port: ' + PORT);
});