From c7a98415aaca04ab420fd217cb043e0060bceffe Mon Sep 17 00:00:00 2001 From: jean-emmanuel <> Date: Sun, 28 Feb 2021 09:41:47 +0100 Subject: [PATCH] docs: custom module example --- resources/docs/docs/custom-module/examples.md | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/resources/docs/docs/custom-module/examples.md b/resources/docs/docs/custom-module/examples.md index 991bc733..884b491c 100644 --- a/resources/docs/docs/custom-module/examples.md +++ b/resources/docs/docs/custom-module/examples.md @@ -178,6 +178,65 @@ module.exports = { } +``` + +## Auto-save client state + +```js +// store state in a variable +var state = {} + +// keep track of connected clients +var clients = [] + +app.on('sessionOpened', (data, client)=>{ + + // client connected (and in a session) + if (!clients.includes(client.id)) clients.push(client.id) + + // when a client opens a session, send the state + // but only if it's the only connected client (otherwise, let the regular sync happen) + if (clients.length === 1) { + receive('/STATE/SET', state, {clientId: clients[0]}) + } + +}) + +app.on('close', (data, client)=>{ + + // client disconnected + if (clients.includes(client.id)) clients.splice(clients.indexOf(client.id)) + +}) + +// request 1st client's state every 10 seconds +setInterval(()=>{ + + if (clients.length > 0) { + receive('/STATE/GET', '127.0.0.1:8888', {clientId: clients[0]}) + } + +}, 10000) + + +module.exports = { + + oscOutFilter: function(data) { + + if (data.address === '/STATE/GET') { + // update state variable when the client responds + state = data.args[0].value + + // bypass the osc message + return + } + + return data + + } + +} + ``` ## MIDI routing