Pass socket creator function instead of url

This commit is contained in:
masad-frost 2019-03-25 16:53:23 -07:00
parent f5d76dd5bb
commit d041b49fd1
2 changed files with 5 additions and 9 deletions

View File

@ -34,18 +34,15 @@ const DISCONNECT_TIMEOUT = 3;
const DEFAULT_BACKGROUND = 'rgb(40, 40, 40)'; const DEFAULT_BACKGROUND = 'rgb(40, 40, 40)';
export default class RFB extends EventTargetMixin { export default class RFB extends EventTargetMixin {
constructor(target, url, options) { constructor(target, websocketCreator, options) {
if (!target) { if (!target) {
throw new Error("Must specify target"); throw new Error("Must specify target");
} }
if (!url) {
throw new Error("Must specify URL");
}
super(); super();
this._target = target; this._target = target;
this._url = url; this._websocketCreator = websocketCreator;
// Connection details // Connection details
options = options || {}; options = options || {};
@ -393,11 +390,10 @@ export default class RFB extends EventTargetMixin {
_connect() { _connect() {
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']); this._sock.open(this._websocketCreator);
} 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

@ -178,10 +178,10 @@ export default class Websock {
this._websocket = null; this._websocket = null;
} }
open(uri, protocols) { open(socketCreator) {
this.init(); this.init();
this._websocket = new WebSocket(uri, protocols); this._websocket = socketCreator();
this._websocket.binaryType = 'arraybuffer'; this._websocket.binaryType = 'arraybuffer';
this._websocket.onmessage = this._recv_message.bind(this); this._websocket.onmessage = this._recv_message.bind(this);