refactor open into open and attach api

Allows us to support already opened channels and websockets we open.
This commit is contained in:
Ryan Castner 2020-04-06 17:09:59 -04:00
parent 62a5ce070e
commit b6cca28838
2 changed files with 16 additions and 23 deletions

View File

@ -501,7 +501,7 @@ export default class RFB extends EventTargetMixin {
} else { } else {
try { try {
Log.Info(`attaching ${this._rawChannel} to WebChannel`); Log.Info(`attaching ${this._rawChannel} to WebChannel`);
this._webchannel.attach(this._rawChannel); this._webchannel.attach(this._rawChannel, true);
} catch (e) { } catch (e) {
this._fail("Error attaching channel (" + e + ")"); this._fail("Error attaching channel (" + e + ")");
} }

View File

@ -25,7 +25,6 @@ const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024; // 40 MiB
export default class WebChannel { export default class WebChannel {
constructor() { constructor() {
this._rawChannel = null; // WebSocket or RTCDataChannel object this._rawChannel = null; // WebSocket or RTCDataChannel object
this._channelType = ""; // Track which type of channel
this._channelStates = null; // Cross compatible states enum for WebSocket / RTCDataChannel this._channelStates = null; // Cross compatible states enum for WebSocket / RTCDataChannel
this._rQi = 0; // Receive queue index this._rQi = 0; // Receive queue index
@ -203,22 +202,19 @@ export default class WebChannel {
this._rawChannel = null; this._rawChannel = null;
} }
open({ uri, protocols, webChannel, channelType }) { open(uri, protocols) {
this.init(); this.init();
if (uri) { let rawChannel = new WebSocket(uri, protocols);
this._rawChannel = new WebSocket(uri, protocols); this.attach(rawChannel, false);
this._channelType = "WebSocket"; }
this._channelStates = this._getChannelStates("WebSocket");
} else if (webChannel && channelType) { attach(rawChannel, isOpen = false) {
this._rawChannel = webChannel; this._rawChannel = rawChannel;
this._channelType = channelType; this._rawChannel.binaryType = "arraybuffer";
this._channelStates = this._getChannelStates(channelType); this._rawChannel.onmessage = this._recv_message.bind(this);
} else {
throw new Error( this._channelStates = this._getChannelStates("WebSocket");
`Expected to receive one of uri and optional protocols or webChannel and channelType`
);
}
const onOpen = () => { const onOpen = () => {
Log.Debug(`>> WebChannel.onopen`); Log.Debug(`>> WebChannel.onopen`);
@ -230,21 +226,18 @@ export default class WebChannel {
Log.Debug(`<< WebChannel.onopen`); Log.Debug(`<< WebChannel.onopen`);
}; };
this._rawChannel.binaryType = "arraybuffer"; if (!isOpen) {
this._rawChannel.onmessage = this._recv_message.bind(this);
if (uri) {
this._rawChannel.onopen = onOpen; this._rawChannel.onopen = onOpen;
} } else {
if (webChannel) {
onOpen(); onOpen();
} }
this._rawChannel.onclose = (e) => { this._rawChannel.onclose = (e) => {
Log.Debug(`>> WebChannel.onclose`); Log.Debug(`>> WebChannel.onclose`);
this._eventHandlers.close(e); this._eventHandlers.close(e);
Log.Debug(`<< WebChannel.onclose`); Log.Debug(`<< WebChannel.onclose`);
}; };
this._rawChannel.onerror = (e) => { this._rawChannel.onerror = (e) => {
Log.Debug(`>> WebChannel.onerror: ` + e); Log.Debug(`>> WebChannel.onerror: ` + e);
this._eventHandlers.error(e); this._eventHandlers.error(e);