13
0

WebSockets: make Message.fromJsonText() a ES6 static method

This commit is contained in:
Luciano Iam 2020-04-12 12:19:11 +02:00 committed by Robin Gareus
parent feeda2c19d
commit 2b8e9ceb89
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04

View File

@ -19,96 +19,96 @@
const JSON_INF = 1.0e+128; const JSON_INF = 1.0e+128;
export function nodeAddressHash(node, addr) { export function nodeAddressHash(node, addr) {
return [node].concat(addr).join('_'); return [node].concat(addr).join('_');
} }
export class MessageChannel { export class MessageChannel {
constructor (host) { constructor (host) {
// https://developer.mozilla.org/en-US/docs/Web/API/URL/host // https://developer.mozilla.org/en-US/docs/Web/API/URL/host
this.host = host; this.host = host;
} }
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.socket.onmessage = (event) => {
this.messageCallback (Message.fromJsonText(event.data)); this.messageCallback (Message.fromJsonText(event.data));
}; };
this.socket.onopen = resolve; this.socket.onopen = resolve;
}); });
} }
send (msg) { send (msg) {
this.socket.send(msg.toJsonText()); this.socket.send(msg.toJsonText());
} }
closeCallback () { closeCallback () {
// empty // empty
} }
errorCallback (error) { errorCallback (error) {
// empty // empty
} }
messageCallback (msg) { messageCallback (msg) {
// empty // empty
} }
} }
export class Message { export class Message {
constructor (node, addr, val) { constructor (node, addr, val) {
this.node = node; this.node = node;
this.addr = addr; this.addr = addr;
this.val = []; this.val = [];
for (const i in val) { for (const i in val) {
if (val[i] >= JSON_INF) { if (val[i] >= JSON_INF) {
this.val.push(Infinity); this.val.push(Infinity);
} else if (val[i] <= -JSON_INF) { } else if (val[i] <= -JSON_INF) {
this.val.push(-Infinity); this.val.push(-Infinity);
} else { } else {
this.val.push(val[i]); this.val.push(val[i]);
} }
} }
} }
static fromJsonText (jsonText) {
let rawMsg = JSON.parse(jsonText);
return new Message(rawMsg.node, rawMsg.addr || [], rawMsg.val);
}
toJsonText () { toJsonText () {
let val = []; let val = [];
for (const i in this.val) { for (const i in this.val) {
if (this.val[i] == Infinity) { if (this.val[i] == Infinity) {
val.push(JSON_INF); val.push(JSON_INF);
} else if (this.val[i] == -Infinity) { } else if (this.val[i] == -Infinity) {
val.push(-JSON_INF); val.push(-JSON_INF);
} else { } else {
val.push(this.val[i]); val.push(this.val[i]);
} }
} }
return JSON.stringify({node: this.node, addr: this.addr, val: val}); return JSON.stringify({node: this.node, addr: this.addr, val: val});
} }
get hash () { get hash () {
return nodeAddressHash(this.node, this.addr); return nodeAddressHash(this.node, this.addr);
} }
toString () { toString () {
return `${this.node} (${this.addr}) = ${this.val}`; return `${this.node} (${this.addr}) = ${this.val}`;
} }
} }
Message.fromJsonText = (jsonText) => {
let rawMsg = JSON.parse(jsonText);
return new Message(rawMsg.node, rawMsg.addr || [], rawMsg.val);
};