-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (49 loc) · 1.66 KB
/
app.js
File metadata and controls
57 lines (49 loc) · 1.66 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
var express = require('express');
var fs = require('fs');
var app = express();
app.use(express.static(__dirname + '/public'));
// Listen for the mjpeg stream request and send JPEG chunks to client.
app.get('/stream.mjpeg', function(request, res){
res.writeHead(200, {
'Content-Type': 'multipart/x-mixed-replace; boundary=--jpegboundary',
'Cache-Control': 'no-cache',
'Connection': 'close',
'Pragma': 'no-cache'
});
var connectionClosed = false;
res.connection.on('close', function() { connectionClosed = true; });
var sendjpeg = function(){
if(connectionClosed){
console.log("Connection has been closed");
return;
}
fs.readFile('/dev/shm/mjpeg/cam.jpg', function(err, content){
if (err) throw err;
res.write("--jpegboundary\r\n");
res.write("Content-Type: image/jpeg\r\n");
res.write("Content-Length: " + content.length + "\r\n");
res.write("\r\n");
res.write(content, 'binary');
res.write("\r\n");
setTimeout(sendjpeg, 100); //10 fps
})
};
sendjpeg();
});
//XHR interface to recaive variuous commands
var fifopath = 'FIFO';
app.get('/xhr/camera', function(req, res){
var command = req.param('cmd');
console.log(command);
var fifostream = fs.createWriteStream(fifopath, {'flags': 'a'});
fifostream.end(command);
fifostream.on('error', function (err) {
console.log(err);
res.send(500);
return;
});
res.send(200);
});
var server = app.listen(3000, function(){
console.log('Listening on port %d', server.address().port);
});