-
Notifications
You must be signed in to change notification settings - Fork 31
/
dgram.js
184 lines (164 loc) · 4.4 KB
/
dgram.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const dgram = typeof __dirname !== 'undefined' ? require('dgram') : undefined
/**
* Status flags
* @private
*/
const STATUS = {
IS_NOT_INITIALIZED: -1,
IS_CONNECTING: 0,
IS_OPEN: 1,
IS_CLOSING: 2,
IS_CLOSED: 3,
}
/**
* Default options for open method
* @private
*/
const defaultOpenOptions = {
host: 'localhost',
port: 41234,
exclusive: false,
}
/**
* Default options for send method
* @private
*/
const defaultSendOptions = {
host: 'localhost',
port: 41235,
}
/**
* Default options
* @private
*/
const defaultOptions = {
type: 'udp4',
open: defaultOpenOptions,
send: defaultSendOptions,
}
/**
* Helper method to merge nested objects
* @private
*/
function mergeOptions(base, custom) {
return {
...defaultOptions,
...base,
...custom,
open: { ...defaultOptions.open, ...base.open, ...custom.open },
send: { ...defaultOptions.send, ...base.send, ...custom.send },
}
}
/**
* OSC plugin for simple OSC messaging via udp client
* and udp server
*/
export default class DatagramPlugin {
/**
* Create an OSC Plugin instance with given options. Defaults to
* localhost:41234 for server and localhost:41235 for client messaging
* @param {object} [options] Custom options
* @param {string} [options.type='udp4'] 'udp4' or 'udp6'
* @param {string} [options.open.host='localhost'] Hostname of udp server to bind to
* @param {number} [options.open.port=41234] Port of udp server to bind to
* @param {boolean} [options.open.exclusive=false] Exclusive flag
* @param {string} [options.send.host='localhost'] Hostname of udp client for messaging
* @param {number} [options.send.port=41235] Port of udp client for messaging
*
* @example
* const plugin = new OSC.DatagramPlugin({ send: { port: 9912 } })
* const osc = new OSC({ plugin: plugin })
*/
constructor(customOptions = {}) {
if (!dgram) {
throw new Error('DatagramPlugin can not be used in browser context')
}
/**
* @type {object} options
* @private
*/
this.options = mergeOptions({}, customOptions)
/**
* @type {object} socket
* @private
*/
this.socket = dgram.createSocket(this.options.type)
/**
* @type {number} socketStatus
* @private
*/
this.socketStatus = STATUS.IS_NOT_INITIALIZED
// register events
this.socket.on('message', (message, rinfo) => {
this.notify(message, rinfo)
})
this.socket.on('error', (error) => {
this.notify('error', error)
})
/**
* @type {function} notify
* @private
*/
this.notify = () => {}
}
/**
* Internal method to hook into osc library's
* EventHandler notify method
* @param {function} fn Notify callback
* @private
*/
registerNotify(fn) {
this.notify = fn
}
/**
* Returns the current status of the connection
* @return {number} Status ID
*/
status() {
return this.socketStatus
}
/**
* Bind a udp socket to a hostname and port
* @param {object} [customOptions] Custom options
* @param {string} [customOptions.host='localhost'] Hostname of udp server to bind to
* @param {number} [customOptions.port=41234] Port of udp server to bind to
* @param {boolean} [customOptions.exclusive=false] Exclusive flag
*/
open(customOptions = {}) {
const options = { ...this.options.open, ...customOptions }
const { port, exclusive } = options
this.socketStatus = STATUS.IS_CONNECTING
this.socket.bind({
address: options.host,
port,
exclusive,
}, () => {
this.socketStatus = STATUS.IS_OPEN
this.notify('open')
})
}
/**
* Close udp socket
*/
close() {
this.socketStatus = STATUS.IS_CLOSING
this.socket.close(() => {
this.socketStatus = STATUS.IS_CLOSED
this.notify('close')
})
}
/**
* Send an OSC Packet, Bundle or Message. Use options here for
* custom port and hostname, otherwise the global options will
* be taken
* @param {Uint8Array} binary Binary representation of OSC Packet
* @param {object} [customOptions] Custom options for udp socket
* @param {string} [customOptions.host] Hostname of udp client
* @param {number} [customOptions.port] Port of udp client
*/
send(binary, customOptions = {}) {
const options = { ...this.options.send, ...customOptions }
const { port, host } = options
this.socket.send(Buffer.from(binary), 0, binary.byteLength, port, host)
}
}