-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·68 lines (62 loc) · 1.84 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
const { Option, program } = require('commander');
const osc = require('osc-min');
const dgram = require('dgram');
const net = require('net');
const packageInfo = require('./package.json');
function argsProcessor(argString, argsArray) {
const cleanArg = {
type: 'string',
value: argString,
};
if (!Number.isNaN(argString * 1)) {
cleanArg.value = argString * 1;
if (argString.includes('.')) {
cleanArg.type = 'float';
} else {
cleanArg.type = 'integer';
}
} else if (cleanArg.value === 'true') {
cleanArg.type = 'boolean';
cleanArg.value = true;
} else if (cleanArg.value === 'false') {
cleanArg.type = 'boolean';
cleanArg.value = false;
}
argsArray.push(cleanArg);
return argsArray;
}
program.name(packageInfo.name);
program.version(packageInfo.version);
program.description('simple util to sendosc');
program.addOption(new Option('-p,--protocol <protocol>', 'Network protocol').choices(['tcp', 'udp']).default('udp'));
program.argument('host', 'the host to send osc to');
program.argument('port', 'the port to send osc to');
program.argument('address', 'OSC address');
program.argument('[args...]', 'optional OSC arguments', argsProcessor, []);
program.action((host, port, address, args, options) => {
const oscMsgBuffer = osc.toBuffer({
address,
args,
});
if (options.protocol === 'tcp') {
const client = net.Socket();
client.on('error', (error) => {
console.error(error);
});
client.connect(port, host, () => {
client.write(oscMsgBuffer, () => {
client.destroy();
});
});
} else if (options.protocol === 'udp') {
const client = dgram.createSocket('udp4');
client.send(oscMsgBuffer, port, host, (error) => {
if (error) {
console.error(error);
}
client.close();
});
}
});
program.parse();