chaosc-fetcher/oscreceiver.js

57 lines
1.6 KiB
JavaScript

// this is a simple osc receiver for displaying osc messages on screen
// to use, simply start with "node oscreceiver.js <hubAddress> <hubPort>"
var readline = require('readline');
var dateFormat = require('dateformat');
var osc = require('node-osc');
var listenPort = 8000;
var hubAddress = process.argv.length > 2 ? process.argv[2] : '192.168.23.43';
var hubPort = process.argv.length > 3 ? process.argv[3] : 7110;
var oscClient = new osc.Client(hubAddress, hubPort);
var oscServer = new osc.Server(listenPort, '0.0.0.0');
require('dns').lookup(require('os').hostname(), function (err, add, fam) {
console.log('my address: '+add + ' subscribing on hub ' + hubAddress);
oscClient.send('/subscribe', add, listenPort, "sekret");
});
oscServer.on("message", function (msg, rinfo) {
var dateString = dateFormat(new Date(), "HH:MM:ss.L");
console.log(dateString + "\t" + msg);
});
var rl = readline.createInterface(process.stdin,process.stdout,null);
rl.on('line', function(cmd) {
var sepIndex = cmd.indexOf(":");
if(sepIndex > 0) {
var path = cmd.substr(0, sepIndex);
var val = cmd.substr(sepIndex+1);
if(isNumber(val)) parseFloat(val);
console.log("writing: %s with %s", path, val);
oscClient.send(path, val);
}
});
rl.on('close', function(cmd) {
console.log("bye bye");
oscClient.send('/unsubscribe', hubAddress, listenPort);
process.exit(0);
});
function isNumber(value) {
if ((undefined === value) || (null === value)) {
return false;
}
if (typeof value == 'number') {
return true;
}
return !isNaN(value - 0);
}