From 93683185871a5e2c2020b28d6aeeb403b7795e29 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 25 Jul 2018 18:23:27 +0200 Subject: [PATCH] custom module: expose `app`, an event listener for monitoring events (see callbacks.js) (#341) --- app/main/osc/index.js | 8 ++++++-- app/main/server.js | 4 ++++ resources/docs/extras/custom-module.md | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/main/osc/index.js b/app/main/osc/index.js index 38232e85..42126949 100644 --- a/app/main/osc/index.js +++ b/app/main/osc/index.js @@ -10,12 +10,15 @@ var ipc = require('../server').ipc, fs = require('fs'), midi = settings.read('midi') ? require('../midi') : false, oscUDPServer = require('./udp'), - oscTCPServer = require('./tcp') + oscTCPServer = require('./tcp'), + EventEmitter = require('events').EventEmitter + class OscServer { constructor(){ + this.customModuleEventEmitter = new EventEmitter() this.customModule = (()=>{ if (!settings.read('customModule')) return false @@ -33,6 +36,7 @@ class OscServer { console, sendOsc: this.sendOsc.bind(this), receiveOsc: this.receiveOsc.bind(this), + app: this.customModuleEventEmitter, setTimeout, clearTimeout, setInterval, @@ -208,7 +212,7 @@ var oscServer = new OscServer() oscServer.init() module.exports = { - + server: oscServer, send: function(host,port,address,args,precision) { var message = [] diff --git a/app/main/server.js b/app/main/server.js index 6a3b0deb..131a0b49 100644 --- a/app/main/server.js +++ b/app/main/server.js @@ -8,6 +8,7 @@ var express = require('express')(), theme = require('./theme').init(), zeroconf = require('./zeroconf'), appAddresses = settings.read('appAddresses'), + osc = require('./osc').server, clients = {} express.get('/', function(req, res){ @@ -45,6 +46,9 @@ var bindCallbacks = function(callbacks) { for (let name in callbacks) { client.on(name, (data)=>{ + if (osc.customModule) { + osc.customModuleEventEmitter.emit(name, data, client.id) + } callbacks[name](data, client.id) }) } diff --git a/resources/docs/extras/custom-module.md b/resources/docs/extras/custom-module.md index a129a3c8..e4f128c8 100644 --- a/resources/docs/extras/custom-module.md +++ b/resources/docs/extras/custom-module.md @@ -54,6 +54,7 @@ The module is executed in a restricted context, only a few globals are available - `clearTimeout`: `function(timeout)` - `setInterval`: `function(function, delay)` - `clearInterval`: `function(interval)` +- `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/app/main/callbacks.js), callbacks are called with 2 arguments: `data` (object) and `clientId` (string) `sendOsc` and `receiveOsc` expect arguments formatted as follow: