custom-module: expose send and receive shorthands, expose client's address in app event handlers

This commit is contained in:
jean-emmanuel 2019-03-20 09:07:54 +01:00
parent 2c45c21bfb
commit abdee0b223
4 changed files with 40 additions and 10 deletions

View file

@ -6,7 +6,10 @@
- added context menu action to display midi device list without starting the server
- widgets
- switch: add `traversing` option (`true` by default)
- custom module
- expose shorthand functions `send` and `receives`
- expose a client object instead of just an id in `app` event handlers to allow retrieving the client's ip address as well
## 0.42.0
- widgets

View file

@ -48,20 +48,41 @@ It must be of the following form:
The module is executed in a restricted context, only a few globals are available :
- `console`: `object`
- `sendOsc`: `function({address, args, host, port})`
- `receiveOsc`: `function({address, args, host, port})`
- `sendOsc`: `function({host, port, address, args})`
- `receiveOsc`: `function({host, port, address, args})`
- `send`: `function(host, port, address, arg1, arg2, ...)`
- `receive`: `function(host, port, address, arg1, arg2, ...)`
- `setTimeout`: `function(function, delay)`
- `clearTimeout`: `function(timeout)`
- `setInterval`: `function(function, delay)`
- `clearInterval`: `function(interval)`
- `settings.read`: `function(name)`, see [settings.js](https://github.com/jean-emmanuel/open-stage-control/blob/master/src/server/settings.js#L55-L103) for available options
- `app`: an [event emitter](https://nodejs.org/api/events.html#events_class_eventemitter) for monitoring the events sent by the different clients. Event names can be found in [callbacks.js](https://github.com/jean-emmanuel/open-stage-control/blob/master/src/server/callbacks.js), callbacks are called with 2 arguments: `data` (object) and `clientId` (string)
- `app`: an [event emitter](https://nodejs.org/api/events.html#events_class_eventemitter) for monitoring the events sent by the different clients. Event names can be found in [callbacks.js](https://github.com/jean-emmanuel/open-stage-control/blob/master/src/server/callbacks.js), callbacks are called with 2 arguments: `data` (object) and `client` (object: `{address, id}`)
- `options`: `array` containing the extra options passed to `--custom-module` after the filename
`sendOsc` and `receiveOsc` expect arguments formatted as follow:
- `address`: `string`
- `args`: `array` of `{type:"OSC_TYPE_LETTER", value:VALUE}` `objects`
- `args`: `array` of `{type: "OSC_TYPE_LETTER", value: VALUE}` `objects`
- `host`: `string` ip address, valid hostname or `midi`
- `port`: `integer` port number or `string` midi device name
`send` and `receive` are shorthands for `sendOsc` and `receiveOsc` that don't require args to be formatted as object (numbers are casted to floats by default):
```js
// calling
send('127.0.0.1', 5555, '/test' 1, 2, {type: 'i', value: 3})
// equals
sendOsc({
host: '127.0.0.1',
port: 5555,
address: '/test',
args: [
{type: 'f', value: 1},
{type: 'f', value: 2},
{type: 'i', value: 3},
]
})
```

View file

@ -44,11 +44,15 @@ class OscServer {
console,
sendOsc: this.sendOsc.bind(this),
receiveOsc: this.receiveOsc.bind(this),
send: (host, port, address, ...args)=>{
this.sendOsc({host, port, address, args:args.map(x=>this.parseArg(x))})
},
receive: (host, port, address, ...args)=>{
this.receiveOsc({host, port, address, args:args.map(x=>this.parseArg(x))})
},
app: this.customModuleEventEmitter,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
setTimeout, clearTimeout,
setInterval, clearInterval,
settings,
options: customModule.slice(1, customModule.length)
}

View file

@ -51,10 +51,12 @@ var bindCallbacks = function(callbacks) {
ipc.on('connection', function(client) {
var clientInfos = {address: client.address, id: client.id}
for (let name in callbacks) {
client.on(name, (data)=>{
if (osc.customModule) {
osc.customModuleEventEmitter.emit(name, data, client.id)
osc.customModuleEventEmitter.emit(name, data, clientInfos)
}
callbacks[name](data, client.id)
})