diff options
author | Luciano Iam <lucianito@gmail.com> | 2020-04-12 12:19:11 +0200 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2020-04-13 16:29:39 +0200 |
commit | 2b8e9ceb891d7a46554d691661e17974989270bc (patch) | |
tree | 817f7078df5262f1d35925f97667aa93e923599e /share/web_surfaces/shared | |
parent | feeda2c19decd2e88e494c1fcc24e940472bbc11 (diff) |
WebSockets: make Message.fromJsonText() a ES6 static method
Diffstat (limited to 'share/web_surfaces/shared')
-rw-r--r-- | share/web_surfaces/shared/channel.js | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/share/web_surfaces/shared/channel.js b/share/web_surfaces/shared/channel.js index 5173cf2c99..89b223ac18 100644 --- a/share/web_surfaces/shared/channel.js +++ b/share/web_surfaces/shared/channel.js @@ -19,96 +19,96 @@ const JSON_INF = 1.0e+128; export function nodeAddressHash(node, addr) { - return [node].concat(addr).join('_'); + return [node].concat(addr).join('_'); } export class MessageChannel { - constructor (host) { - // https://developer.mozilla.org/en-US/docs/Web/API/URL/host - this.host = host; - } + constructor (host) { + // https://developer.mozilla.org/en-US/docs/Web/API/URL/host + this.host = host; + } - open () { - return new Promise((resolve, reject) => { - this.socket = new WebSocket(`ws://${this.host}`); + open () { + return new Promise((resolve, reject) => { + 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.messageCallback (Message.fromJsonText(event.data)); - }; + 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()); - } + send (msg) { + this.socket.send(msg.toJsonText()); + } - closeCallback () { - // empty - } - - errorCallback (error) { - // empty - } + closeCallback () { + // empty + } + + errorCallback (error) { + // empty + } - messageCallback (msg) { - // empty - } + messageCallback (msg) { + // empty + } } export class Message { - constructor (node, addr, val) { - this.node = node; - this.addr = addr; - this.val = []; - - for (const i in val) { - if (val[i] >= JSON_INF) { - this.val.push(Infinity); - } else if (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]); - } - } - - return JSON.stringify({node: this.node, addr: this.addr, val: val}); - } - - get hash () { - return nodeAddressHash(this.node, this.addr); - } - - toString () { - return `${this.node} (${this.addr}) = ${this.val}`; - } + constructor (node, addr, val) { + this.node = node; + this.addr = addr; + this.val = []; + + for (const i in val) { + if (val[i] >= JSON_INF) { + this.val.push(Infinity); + } else if (val[i] <= -JSON_INF) { + this.val.push(-Infinity); + } else { + this.val.push(val[i]); + } + } + } + + static fromJsonText (jsonText) { + let rawMsg = JSON.parse(jsonText); + return new Message(rawMsg.node, rawMsg.addr || [], rawMsg.val); + } + + 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]); + } + } + + return JSON.stringify({node: this.node, addr: this.addr, val: val}); + } + + get hash () { + return nodeAddressHash(this.node, this.addr); + } + + toString () { + return `${this.node} (${this.addr}) = ${this.val}`; + } } - -Message.fromJsonText = (jsonText) => { - let rawMsg = JSON.parse(jsonText); - return new Message(rawMsg.node, rawMsg.addr || [], rawMsg.val); -}; |