-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (54 loc) · 1.36 KB
/
Copy pathindex.js
File metadata and controls
69 lines (54 loc) · 1.36 KB
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
/**
* Module dependencies.
*/
var debug = require('debug')('osc-receiver');
var dgram = require('dgram');
var osc = require('osc-min');
var EventEmitter = require('events').EventEmitter;
/**
* Expose `OscReceiver`.
*/
module.exports = OscReceiver;
/**
* OscReceiver constructor.
*/
function OscReceiver() {
this._socket = dgram.createSocket('udp4');
this._socket.on('message', this._onMessage.bind(this));
}
/**
* Inherits from `EventEmitter`.
*/
OscReceiver.prototype.__proto__ = EventEmitter.prototype;
/**
* @param {Number} port
* @param {String} address
* @param {Function} callback
*/
OscReceiver.prototype.bind = function(port, address, callback) {
this._socket.bind.apply(this._socket, arguments);
return this;
};
/**
* @param {Object} msg
* @param {Object} rInfo
*/
OscReceiver.prototype._onMessage = function(msg, rInfo) {
try {
var message = osc.fromBuffer(msg);
} catch (e) {
return this.emit('error', e);
}
var self = this;
var elements = 'bundle' === message.oscType ? message.elements : [message];
elements.forEach(function(el, index) {
var args = [el.address];
el.args.forEach(function(arg, index) {
args.push(arg.value);
});
debug('receive %j from %s:%s', args, rInfo.address, rInfo.port);
self.emit.apply(self, ['message'].concat(args));
self.emit.apply(self, args);
});
return this;
};