forked from devalexqt/pi_h264
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
73 lines (55 loc) · 1.52 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
69
70
71
72
73
const http = require("http");
const os = require("os");
const fs = require("fs");
const streamer = require("./streamer.js");
const config = require('./config.json');
let port = config.port;
console.log("Starting server on +:" + port);
let server = http.createServer(function(req,res) {
console.log("==> new connection: " + req.connection.remoteAddress + ", url: " + req.url);
let filename = req.url == "/" ? "/index.html" : req.url;
let method = req.method.toLowerCase();
if(method === "get") {
let file = fs.createReadStream("www" + filename);
file.on("error", function(err) {
console.error("Failed to open file", err)
});
file.pipe(res);
}
}).listen(port);
let stream;
// socket.io
let io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.emit('data', { data: 'Hi from server' });
socket.on('data', function(data) {
console.log(data);
});
socket.on("start_stream", function(data) {
console.log("==> start_stream");
startStream(socket);
});
socket.on("stop_stream", function(data) {
console.log("==> stop_stream");
stopStream();
});
});
let currentlyStreaming = false;
function startStream(socket) {
// if stream is already running
// stop that one for the new one
if(currentlyStreaming) {
stopStream(socket);
}
currentlyStreaming = true;
stream = new streamer.start(socket);
}
function stopStream(socket) {
console.log("==> stoping stream");
try {
stream.kill();
} catch(err) {
console.error(err);
}
}
console.log(os.networkInterfaces());