-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
86 lines (68 loc) · 1.63 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Module dependencies.
*/
var debug = require('debug')('osc-emitter')
, dgram = require('dgram')
, osc = require('osc-min')
, EventEmitter = require('events').EventEmitter;
/**
* Expose `OscEmitter`.
*/
module.exports = OscEmitter;
/**
* `OscEmitter` constructor.
*/
function OscEmitter() {
this._receivers = [];
this._socket = dgram.createSocket('udp4');
}
/**
* Inherits from `EventEmitter`.
*/
OscEmitter.prototype.__proto__ = EventEmitter.prototype;
/**
* `EventEmitter#emit` recerence.
*/
var emit = EventEmitter.prototype.emit;
/**
* Emit a OSC message.
*
* @param {String} address
* @param {Mixed} argument
*/
OscEmitter.prototype.emit = function() {
if (arguments.length < 1) {
throw new TypeError('1 or more argument required.');
}
var socket = this._socket;
var args = Array.prototype.slice.call(arguments);
var message = osc.toBuffer({ address: args[0], args: args.slice(1) });
this._receivers.forEach(function(receiver) {
debug('send %j to %s:%s', args, receiver.host, receiver.port);
socket.send(message, 0, message.length, receiver.port, receiver.host);
});
return this;
};
/**
* Add a receiver.
*
* @param {String} host
* @param {Number} port
*/
OscEmitter.prototype.add = function(host, port) {
this.remove(host, port);
this._receivers.push({ host: host, port: port });
return this;
};
/**
* Remove a receiver.
*
* @param {String} host
* @param {Number} port
*/
OscEmitter.prototype.remove = function(host, port) {
this._receivers = this._receivers.filter(function(receiver) {
return !(receiver.host === host && receiver.port === port);
});
return this;
};