172 lines
4.7 KiB
JavaScript
172 lines
4.7 KiB
JavaScript
// Muaz Khan - www.MuazKhan.com
|
|
// MIT License - www.WebRTC-Experiment.com/licence
|
|
// Documentation - github.com/muaz-khan/FileBufferReader
|
|
|
|
var port = 9001;
|
|
|
|
port == 9001 && process.argv.forEach(function (val, index, array) {
|
|
if(val.toString().indexOf('port=') != -1) {
|
|
val = val.toString().split('=')[1];
|
|
val = parseInt(val);
|
|
port = val;
|
|
}
|
|
});
|
|
|
|
var fs = require('fs');
|
|
var path = require('path');
|
|
|
|
// force auto reboot on failures
|
|
var autoRebootServerOnFailure = true;
|
|
|
|
var server = require('http');
|
|
var url = require('url');
|
|
|
|
function serverHandler(request, response) {
|
|
try {
|
|
var uri = url.parse(request.url).pathname,
|
|
filename = path.join(process.cwd(), uri);
|
|
|
|
var stats;
|
|
|
|
try {
|
|
stats = fs.lstatSync(filename);
|
|
} catch (e) {
|
|
response.writeHead(404, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('404 Not Found: ' + path.join('/', uri) + '\n');
|
|
response.end();
|
|
return;
|
|
}
|
|
|
|
var contentType;
|
|
|
|
if (fs.statSync(filename).isDirectory()) {
|
|
contentType = {
|
|
'Content-Type': 'text/html'
|
|
};
|
|
|
|
filename += '/index.html';
|
|
}
|
|
|
|
|
|
fs.readFile(filename, 'binary', function(err, file) {
|
|
if (err) {
|
|
response.writeHead(500, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('404 Not Found: ' + path.join('/', uri) + '\n');
|
|
response.end();
|
|
return;
|
|
}
|
|
|
|
response.writeHead(200, contentType);
|
|
response.write(file, 'binary');
|
|
response.end();
|
|
});
|
|
} catch (e) {
|
|
response.writeHead(404, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('<h1>Unexpected error:</h1><br><br>' + e.stack || e.message || JSON.stringify(e));
|
|
response.end();
|
|
}
|
|
}
|
|
|
|
var app = server.createServer(serverHandler);
|
|
|
|
function cmd_exec(cmd, args, cb_stdout, cb_end) {
|
|
var spawn = require('child_process').spawn,
|
|
child = spawn(cmd, args),
|
|
me = this;
|
|
me.exit = 0;
|
|
me.stdout = "";
|
|
child.stdout.on('data', function(data) {
|
|
cb_stdout(me, data)
|
|
});
|
|
child.stdout.on('end', function() {
|
|
cb_end(me)
|
|
});
|
|
}
|
|
|
|
function log_console() {
|
|
console.log(foo.stdout);
|
|
|
|
try {
|
|
var pidToBeKilled = foo.stdout.split('\nnode ')[1].split(' ')[0];
|
|
console.log('------------------------------');
|
|
console.log('Please execute below command:');
|
|
console.log('\x1b[31m%s\x1b[0m ', 'kill ' + pidToBeKilled);
|
|
console.log('Then try to run "server.js" again.');
|
|
console.log('------------------------------');
|
|
|
|
} catch (e) {}
|
|
}
|
|
|
|
function runServer() {
|
|
app.on('error', function(e) {
|
|
if (e.code == 'EADDRINUSE') {
|
|
if (e.address === '0.0.0.0') {
|
|
e.address = 'localhost';
|
|
}
|
|
|
|
var socketURL = 'http://' + e.address + ':' + e.port + '/';
|
|
|
|
console.log('------------------------------');
|
|
console.log('\x1b[31m%s\x1b[0m ', 'Unable to listen on port: ' + e.port);
|
|
console.log('\x1b[31m%s\x1b[0m ', socketURL + ' is already in use. Please kill below processes using "kill PID".');
|
|
console.log('------------------------------');
|
|
|
|
foo = new cmd_exec('lsof', ['-n', '-i4TCP:9001'],
|
|
function(me, data) {
|
|
me.stdout += data.toString();
|
|
},
|
|
function(me) {
|
|
me.exit = 1;
|
|
}
|
|
);
|
|
|
|
setTimeout(log_console, 250);
|
|
}
|
|
});
|
|
|
|
app = app.listen(port, process.env.IP || '0.0.0.0', function(error) {
|
|
var addr = app.address();
|
|
|
|
if (addr.address === '0.0.0.0') {
|
|
addr.address = 'localhost';
|
|
}
|
|
|
|
var domainURL = 'http://' + addr.address + ':' + addr.port + '/';
|
|
|
|
console.log('------------------------------');
|
|
|
|
console.log('socket.io is listening at:');
|
|
console.log('\x1b[31m%s\x1b[0m ', '\t' + domainURL);
|
|
});
|
|
|
|
var io = require('socket.io')(app);
|
|
io.on('connection', function(socket){
|
|
socket.on('buffer-stream', function(buffer) {
|
|
socket.broadcast.emit('buffer-stream', buffer);
|
|
});
|
|
});
|
|
}
|
|
|
|
if (autoRebootServerOnFailure) {
|
|
// auto restart app on failure
|
|
var cluster = require('cluster');
|
|
if (cluster.isMaster) {
|
|
cluster.fork();
|
|
|
|
cluster.on('exit', function(worker, code, signal) {
|
|
cluster.fork();
|
|
});
|
|
}
|
|
|
|
if (cluster.isWorker) {
|
|
runServer();
|
|
}
|
|
} else {
|
|
runServer();
|
|
} |