diff options
author | Tim Stableford <tims@bsquare.com> | 2021-02-18 08:42:34 +0000 |
---|---|---|
committer | Tim Stableford <tims@bsquare.com> | 2021-03-04 18:55:06 +0000 |
commit | 44d384b99c78d39601820be1890b83b35847fc7b (patch) | |
tree | d5da1b8a8b39c08dd3b69f8cf8ef7f3cf82f5880 /core/rfb.js | |
parent | 5a0cceb8156926fe125ca541adae60754ecd3698 (diff) | |
download | novnc-44d384b99c78d39601820be1890b83b35847fc7b.tar.gz |
Added support for RTCDataChannel
This work is originally by Ryan Castner <castner.rr@gmail.com> and
submitted as a PR here https://github.com/novnc/noVNC/pull/1362
Architecturally it is much the same except it doesn't rename a lot
of variables to make this more reviewable. It also avoids unrelated
changes such as replacing .onclose with an event listener, which
caused numerous test failures.
It also adds in ppoffice's fix to initialise the buffers.
Like the original author I don't have enough time available to
refactor this project to the new style event listeners.
Review cleanup for RTCDataChannel support (see below)
* More descriptive error when url or channel not set.
* Moved websocket property check to WebSock.
This had unintended consequences in the tests that required some
fixup. Mostly due to some tests not always passing FakeWebsocket.
FakeWebsocket also needs to set the listeners to null to be compatible
with what is in thw browser and expected by the property check code.
The property check code now also takes into account class prototypes
for test compatibility.
* Removed unreachable code.
* Reverted comment.
* Cleanup raw channel reference in rfb on websock close.
* Use readyState to check whether a socket is open rather than assuming.
* Updated RFB constructor documentation
Removed an unused boolean passed to attach
Diffstat (limited to 'core/rfb.js')
-rw-r--r-- | core/rfb.js | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/core/rfb.js b/core/rfb.js index 26cdfcd..b040739 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -66,20 +66,25 @@ const extendedClipboardActionPeek = 1 << 26; const extendedClipboardActionNotify = 1 << 27; const extendedClipboardActionProvide = 1 << 28; - export default class RFB extends EventTargetMixin { - constructor(target, url, options) { + constructor(target, urlOrChannel, options) { if (!target) { throw new Error("Must specify target"); } - if (!url) { - throw new Error("Must specify URL"); + if (!urlOrChannel) { + throw new Error("Must specify URL, WebSocket or RTCDataChannel"); } super(); this._target = target; - this._url = url; + + if (typeof urlOrChannel === "string") { + this._url = urlOrChannel; + } else { + this._url = null; + this._rawChannel = urlOrChannel; + } // Connection details options = options || {}; @@ -275,6 +280,8 @@ export default class RFB extends EventTargetMixin { break; } this._sock.off('close'); + // Delete reference to raw channel to allow cleanup. + this._rawChannel = null; }); this._sock.on('error', e => Log.Warn("WebSocket on-error event")); @@ -501,16 +508,23 @@ export default class RFB extends EventTargetMixin { _connect() { Log.Debug(">> RFB.connect"); - Log.Info("connecting to " + this._url); - - try { - // WebSocket.onopen transitions to the RFB init states - this._sock.open(this._url, this._wsProtocols); - } catch (e) { - if (e.name === 'SyntaxError') { - this._fail("Invalid host or port (" + e + ")"); - } else { - this._fail("Error when opening socket (" + e + ")"); + if (this._url) { + try { + Log.Info(`connecting to ${this._url}`); + this._sock.open(this._url, this._wsProtocols); + } catch (e) { + if (e.name === 'SyntaxError') { + this._fail("Invalid host or port (" + e + ")"); + } else { + this._fail("Error when opening socket (" + e + ")"); + } + } + } else { + try { + Log.Info(`attaching ${this._rawChannel} to Websock`); + this._sock.attach(this._rawChannel); + } catch (e) { + this._fail("Error attaching channel (" + e + ")"); } } |