This commit is contained in:
NaNuNoo 2018-07-08 12:40:39 +00:00 committed by GitHub
commit 1a0b3a1877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 25 deletions

View File

@ -28,16 +28,16 @@ import "./util/polyfill.js";
// How many seconds to wait for a disconnect to finish // How many seconds to wait for a disconnect to finish
const DISCONNECT_TIMEOUT = 3; const DISCONNECT_TIMEOUT = 3;
export default function RFB(target, url, options) { export default function RFB(target, urlOrChannel, options) {
if (!target) { if (!target) {
throw Error("Must specify target"); throw Error("Must specify target");
} }
if (!url) { if (!urlOrChannel) {
throw Error("Must specify URL"); throw Error("Must specify URL or WebSocket or RTCWebChannel");
} }
this._target = target; this._target = target;
this._url = url; this._urlOrChannel = urlOrChannel;
// Connection details // Connection details
options = options || {}; options = options || {};
@ -394,11 +394,15 @@ RFB.prototype = {
_connect: function () { _connect: function () {
Log.Debug(">> RFB.connect"); Log.Debug(">> RFB.connect");
Log.Info("connecting to " + this._url);
try { try {
// WebSocket.onopen transitions to the RFB init states // WebSocket.onopen transitions to the RFB init states
this._sock.open(this._url, ['binary']); if (typeof(this._urlOrChannel) === "string") {
Log.Info("connecting to " + this._urlOrChannel);
this._sock.open(this._urlOrChannel, ['binary']);
} else {
Log.Info("use established WebSocket or WebRTC connection");
this._sock.open(this._urlOrChannel);
}
} catch (e) { } catch (e) {
if (e.name === 'SyntaxError') { if (e.name === 'SyntaxError') {
this._fail("Invalid host or port (" + e + ")"); this._fail("Invalid host or port (" + e + ")");

View File

@ -17,7 +17,7 @@ import * as Log from './util/logging.js';
export default function Websock() { export default function Websock() {
"use strict"; "use strict";
this._websocket = null; // WebSocket object this._webChannel = null; // WebSocket or RTCDataChannel object
this._rQi = 0; // Receive queue index this._rQi = 0; // Receive queue index
this._rQlen = 0; // Next write position in the receive queue this._rQlen = 0; // Next write position in the receive queue
@ -154,8 +154,8 @@ Websock.prototype = {
// Send Queue // Send Queue
flush: function () { flush: function () {
if (this._sQlen > 0 && this._websocket.readyState === WebSocket.OPEN) { if (this._sQlen > 0 && this._webChannel.readyState === WebSocket.OPEN) {
this._websocket.send(this._encode_message()); this._webChannel.send(this._encode_message());
this._sQlen = 0; this._sQlen = 0;
} }
}, },
@ -189,46 +189,61 @@ Websock.prototype = {
init: function () { init: function () {
this._allocate_buffers(); this._allocate_buffers();
this._rQi = 0; this._rQi = 0;
this._websocket = null; this._webChannel = null;
}, },
open: function (uri, protocols) { // call open method through three different ways below:
// websock.open(uri, protocols)
// websock.open(webSocket)
// websock.open(rtcDataChannel)
open: function () {
this.init(); this.init();
this._websocket = new WebSocket(uri, protocols); if (arguments.length >= 2) {
this._websocket.binaryType = 'arraybuffer'; const uri = arguments[0];
const protocols = arguments[1];
this._webChannel = new WebSocket(uri, protocols);
} else {
this._webChannel = arguments[0];
}
this._websocket.onmessage = this._recv_message.bind(this); this._webChannel.binaryType = 'arraybuffer';
this._websocket.onopen = (function () {
this._webChannel.onmessage = this._recv_message.bind(this);
this._webChannel.onopen = (function () {
Log.Debug('>> WebSock.onopen'); Log.Debug('>> WebSock.onopen');
if (this._websocket.protocol) { if (this._webChannel.protocol) {
Log.Info("Server choose sub-protocol: " + this._websocket.protocol); Log.Info("Server choose sub-protocol: " + this._webChannel.protocol);
} }
this._eventHandlers.open(); this._eventHandlers.open();
Log.Debug("<< WebSock.onopen"); Log.Debug("<< WebSock.onopen");
}).bind(this); }).bind(this);
this._websocket.onclose = (function (e) { this._webChannel.onclose = (function (e) {
Log.Debug(">> WebSock.onclose"); Log.Debug(">> WebSock.onclose");
this._eventHandlers.close(e); this._eventHandlers.close(e);
Log.Debug("<< WebSock.onclose"); Log.Debug("<< WebSock.onclose");
}).bind(this); }).bind(this);
this._websocket.onerror = (function (e) { this._webChannel.onerror = (function (e) {
Log.Debug(">> WebSock.onerror: " + e); Log.Debug(">> WebSock.onerror: " + e);
this._eventHandlers.error(e); this._eventHandlers.error(e);
Log.Debug("<< WebSock.onerror: " + e); Log.Debug("<< WebSock.onerror: " + e);
}).bind(this); }).bind(this);
if (this._webChannel.readyState !== "connecting") {
setTimeout(this._webChannel.onopen, 0);
}
}, },
close: function () { close: function () {
if (this._websocket) { if (this._webChannel) {
if ((this._websocket.readyState === WebSocket.OPEN) || if ((this._webChannel.readyState === WebSocket.OPEN) ||
(this._websocket.readyState === WebSocket.CONNECTING)) { (this._webChannel.readyState === WebSocket.CONNECTING)) {
Log.Info("Closing WebSocket connection"); Log.Info("Closing WebSocket connection");
this._websocket.close(); this._webChannel.close();
} }
this._websocket.onmessage = function (e) { return; }; this._webChannel.onmessage = function (e) { return; };
} }
}, },