41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
var events = require('events');
|
|
var net = require('net');
|
|
|
|
var listener = module.exports = function listener(port){
|
|
|
|
var _this = this;
|
|
|
|
var emitLog = function(text){
|
|
_this.emit('log', text);
|
|
};
|
|
|
|
|
|
this.start = function(){
|
|
net.createServer(function(c) {
|
|
|
|
var data = '';
|
|
try {
|
|
c.on('data', function (d) {
|
|
data += d;
|
|
if (data.slice(-1) === '\n') {
|
|
c.end();
|
|
}
|
|
});
|
|
c.on('end', function () {
|
|
var message = JSON.parse(data);
|
|
_this.emit('command', message.command, message.params, message.options);
|
|
});
|
|
}
|
|
catch(e){
|
|
emitLog('CLI listener failed to parse message ' + data);
|
|
}
|
|
|
|
}).listen(port, '127.0.0.1', function() {
|
|
emitLog('CLI listening on port ' + port)
|
|
});
|
|
}
|
|
|
|
};
|
|
|
|
listener.prototype.__proto__ = events.EventEmitter.prototype;
|