UNPKG

monode

Version:

Toolkit for the monome and arc osc controllers

65 lines (59 loc) 1.67 kB
(function() { var nodeOsc, usedPorts; nodeOsc = require('node-osc'); usedPorts = {}; exports.Client = function(port, host) { if (host == null) { host = '127.0.0.1'; } return new nodeOsc.Client(host, port); }; exports.Server = function(port, callback, host) { var attemptCount, findOpenServer, maxAttempts, server; if (port == null) { port = 8000; } if (host == null) { host = '127.0.0.1'; } server = null; attemptCount = 0; maxAttempts = 10; findOpenServer = function(check) { var client, rand, timeout1, timeout2; attemptCount += 1; if (usedPorts[check]) { return findOpenServer[check + 1]; } usedPorts[check] = true; rand = 'test:' + Math.random(); timeout2 = null; timeout1 = setTimeout(function() { return timeout2 = setTimeout(function() { if (attemptCount > maxAttempts) { return callback(new Error('Failed to find available port')); } else { return findOpenServer(check + 1); } }, 8); }, 8); try { server = new nodeOsc.Server(check, host); server.on('message', function(msg, rinfo) { if (msg[1] === rand) { if (timeout2) { clearTimeout(timeout2); } if (timeout1) { clearTimeout(timeout1); } return callback(null, server); } }); client = new nodeOsc.Client(host, check); return client.send('/random/test', rand); } catch (_error) {} }; return findOpenServer(port); }; }).call(this);