WebSockets: detect channel drop in ardour.js
This commit is contained in:
parent
236ba5c1c4
commit
1e3084d760
@ -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();
|
||||
|
@ -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()');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user