refactor open into open and attach api
Allows us to support already opened channels and websockets we open.
This commit is contained in:
parent
62a5ce070e
commit
b6cca28838
|
|
@ -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 + ")");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,23 +202,20 @@ 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) {
|
|
||||||
this._rawChannel = webChannel;
|
|
||||||
this._channelType = channelType;
|
|
||||||
this._channelStates = this._getChannelStates(channelType);
|
|
||||||
} else {
|
|
||||||
throw new Error(
|
|
||||||
`Expected to receive one of uri and optional protocols or webChannel and channelType`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attach(rawChannel, isOpen = false) {
|
||||||
|
this._rawChannel = rawChannel;
|
||||||
|
this._rawChannel.binaryType = "arraybuffer";
|
||||||
|
this._rawChannel.onmessage = this._recv_message.bind(this);
|
||||||
|
|
||||||
|
this._channelStates = this._getChannelStates("WebSocket");
|
||||||
|
|
||||||
const onOpen = () => {
|
const onOpen = () => {
|
||||||
Log.Debug(`>> WebChannel.onopen`);
|
Log.Debug(`>> WebChannel.onopen`);
|
||||||
if (this._rawChannel.protocol) {
|
if (this._rawChannel.protocol) {
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue