13
0

WebSockets: decouple Message from MessageChannel

This commit is contained in:
Luciano Iam 2020-04-12 12:01:41 +02:00 committed by Robin Gareus
parent a51ce18334
commit 22199e8845
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
3 changed files with 69 additions and 37 deletions

View File

@ -16,7 +16,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
import { MessageChannel } from '/shared/channel.js'; import { MessageChannel, Message } from '/shared/channel.js';
import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider, import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider,
StripPanSlider, StripGainSlider, StripMeter } from './widget.js'; StripPanSlider, StripGainSlider, StripMeter } from './widget.js';
@ -33,18 +33,18 @@ import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider,
main(); main();
function main () { function main () {
channel.messageCallback = (node, addr, val) => { channel.messageCallback = (msg) => {
log(`${node} (${addr}) = ${val}`, 'message-in'); log(`${msg}`, 'message-in');
if (node == 'strip_desc') { if (msg.node == 'strip_desc') {
createStrip (addr, ...val); createStrip (msg.addr, ...msg.val);
} else if (node == 'strip_plugin_desc') { } else if (msg.node == 'strip_plugin_desc') {
createStripPlugin (addr, ...val); createStripPlugin (msg.addr, ...msg.val);
} else if (node == 'strip_plugin_param_desc') { } else if (msg.node == 'strip_plugin_param_desc') {
createStripPluginParam (addr, ...val); createStripPluginParam (msg.addr, ...msg.val);
} else if (FEEDBACK_NODES.includes(node)) { } else if (FEEDBACK_NODES.includes(msg.node)) {
if (widgets[[node, addr]]) { if (widgets[[msg.node, msg.addr]]) {
widgets[[node, addr]].value = val[0]; widgets[[msg.node, msg.addr]].value = msg.val[0];
} }
} }
}; };
@ -127,9 +127,9 @@ import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider,
} }
function send (widget) { function send (widget) {
const val = widget.value; const msg = new Message(widget.node, widget.addr, [widget.value]);
log(`${widget.node} (${widget.addr}) = ${val}`, 'message-out'); log(`${msg}`, 'message-out');
channel.send(widget.node, widget.addr, [val]); channel.send(msg);
} }
function createElem (html, parent) { function createElem (html, parent) {

View File

@ -39,7 +39,7 @@ export class Widget {
} }
get hash () { get hash () {
return [this.node, this.addr]; return [this.node].concat(this.addr).join('_');
} }
} }

View File

@ -28,13 +28,23 @@ export class MessageChannel {
open () { open () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.socket = new WebSocket(`ws://${this.host}`); this.socket = new WebSocket(`ws://${this.host}`);
this.socket.onclose = () => this.closeCallback(); this.socket.onclose = () => this.closeCallback();
this.socket.onerror = (error) => this.errorCallback(error); this.socket.onerror = (error) => this.errorCallback(error);
this.socket.onmessage = (event) => this._onMessage(event);
this.socket.onmessage = (event) => {
this.messageCallback (Message.fromJsonText(event.data));
};
this.socket.onopen = resolve; this.socket.onopen = resolve;
}); });
} }
send (msg) {
this.socket.send(msg.toJsonText());
}
closeCallback () { closeCallback () {
// empty // empty
} }
@ -43,36 +53,58 @@ export class MessageChannel {
// empty // empty
} }
messageCallback (node, addr, val) { messageCallback (msg) {
// empty // empty
} }
send (node, addr, val) { }
export class Message {
constructor (node, addr, val) {
this.node = node;
this.addr = addr;
this.val = [];
for (const i in val) { for (const i in val) {
if (val[i] == Infinity) { if (val[i] >= JSON_INF) {
val[i] = JSON_INF; this.val.push(Infinity);
} else if (val[i] == -Infinity) { } else if (val[i] <= -JSON_INF) {
val[i] = -JSON_INF; this.val.push(-Infinity);
} else {
this.val.push(val[i]);
}
}
}
toJsonText () {
let val = [];
for (const i in this.val) {
if (this.val[i] == Infinity) {
val.push(JSON_INF);
} else if (this.val[i] == -Infinity) {
val.push(-JSON_INF);
} else {
val.push(this.val[i]);
} }
} }
const json = JSON.stringify({node: node, addr: addr, val: val}); return JSON.stringify({node: this.node, addr: this.addr, val: val});
this.socket.send(json);
} }
_onMessage (event) { get hash () {
const msg = JSON.parse(event.data); return [this.node].concat(this.addr).join('_');
}
for (const i in msg.val) { toString () {
if (msg.val[i] >= JSON_INF) { return `${this.node} (${this.addr}) = ${this.val}`;
msg.val[i] = Infinity;
} else if (msg.val[i] <= -JSON_INF) {
msg.val[i] = -Infinity;
}
}
this.messageCallback(msg.node, msg.addr || [], msg.val);
} }
} }
Message.fromJsonText = (jsonText) => {
let rawMsg = JSON.parse(jsonText);
return new Message(rawMsg.node, rawMsg.addr || [], rawMsg.val);
};