forked from licson/node-YouTubeStreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (41 loc) · 1.35 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
// # node-YouTubeStreamer
// _by Licson Lee <[email protected]>_
//
// Streams YouTube video to clients using a proxy-like implantation.
// Load required modules in
var http = require('http');
var streamer = require('./index.js');
// node.js cluster module
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
// Where should we listen for requests?
var IP = process.argv[2] || '127.0.0.1';
var PORT = Number(process.argv[3] || 8000);
// If this is the master process, fork child process
if(cluster.isMaster){
// Fork based on the number of CPUs
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
// Check for the child's states and restart them if died
cluster.on('exit', function(worker, code) {
console.log('Worker ' + worker.process.pid + ' died with code '+code);
cluster.fork();
});
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is ready!');
});
// node.js 0.6 compatibility
cluster.on('death', function(worker, code) {
console.log('Worker ' + worker.process.pid + ' died with code '+code);
cluster.fork();
});
}
else{
// We're in the child process. Start the HTTP server.
var server = http.createServer(streamer());
// Listen on the specified IP and port.
server.listen(PORT,IP,function(){
console.log('Server #%d listening at %s:%d',process.pid,IP,PORT);
});
}