From 1e3084d7604dbe725d57d227921f3b4290c16265 Mon Sep 17 00:00:00 2001 From: Luciano Iam Date: Sun, 12 Apr 2020 15:36:51 +0200 Subject: WebSockets: detect channel drop in ardour.js --- share/web_surfaces/shared/ardour.js | 40 +++++++++++++++++++++++++++--------- share/web_surfaces/shared/channel.js | 20 ++++++++++-------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/share/web_surfaces/shared/ardour.js b/share/web_surfaces/shared/ardour.js index b5ce9b1c1d..770881b4c4 100644 --- a/share/web_surfaces/shared/ardour.js +++ b/share/web_surfaces/shared/ardour.js @@ -21,17 +21,27 @@ import { MessageChannel, Message, ANode } from './channel.js'; export class Ardour { constructor () { - this.channel = new MessageChannel(location.host); - this.channel.messageCallback = (msg) => this._onChannelMessage(msg); - this.pendingRequest = null; + this._channel = new MessageChannel(location.host); + this._channel.errorCallback = (error) => this.errorCallback(); + this._channel.messageCallback = (msg) => this._onChannelMessage(msg); + this._pendingRequest = null; } async open () { - await this.channel.open(); + this._channel.closeCallback = () => { + this.errorCallback(new Error('Message channel unexpectedly closed')); + }; + + await this._channel.open(); } close () { - this.channel.close(); + this._channel.closeCallback = () => {}; + this._channel.close(); + } + + errorCallback (error) { + // empty } messageCallback (msg) { @@ -120,21 +130,21 @@ export class Ardour { _send (node, addr, val) { const msg = new Message(node, addr, val); - this.channel.send(msg); + this._channel.send(msg); return msg; } async _sendAndReceive (node, addr, val) { return new Promise((resolve, reject) => { const hash = this._send(node, addr, val).hash; - this.pendingRequest = {resolve: resolve, hash: hash}; + this._pendingRequest = {resolve: resolve, hash: hash}; }); } _onChannelMessage (msg) { - if (this.pendingRequest && (this.pendingRequest.hash == msg.hash)) { - this.pendingRequest.resolve(msg.val); - this.pendingRequest = null; + if (this._pendingRequest && (this._pendingRequest.hash == msg.hash)) { + this._pendingRequest.resolve(msg.val); + this._pendingRequest = null; } else { this.messageCallback(msg); } @@ -145,3 +155,13 @@ export class Ardour { } } + +async function main() { + const ard = new Ardour(); + ard.errorCallback = (error) => { + alert(error); + }; + await ard.open(); +} + +main(); diff --git a/share/web_surfaces/shared/channel.js b/share/web_surfaces/shared/channel.js index 03b87979d2..8c6f8afdcd 100644 --- a/share/web_surfaces/shared/channel.js +++ b/share/web_surfaces/shared/channel.js @@ -41,28 +41,32 @@ export class MessageChannel { constructor (host) { // https://developer.mozilla.org/en-US/docs/Web/API/URL/host - this.host = host; + this._host = host; } async open () { 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.socket.onopen = resolve; + this._socket.onopen = resolve; }); } + async close () { + this._socket.close(); + } + send (msg) { - if (this.socket) { - this.socket.send(msg.toJsonText()); + if (this._socket) { + this._socket.send(msg.toJsonText()); } else { throw Error('MessageChannel: cannot call send() before open()'); } -- cgit v1.2.3