diff --git a/core/rfb.js b/core/rfb.js index 76d13f0a..f8fc4e71 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -183,22 +183,13 @@ export default class RFB extends EventTargetMixin { this._sock = new Websock(); this._sock.on('message', this._handle_message.bind(this)); this._sock.on('open', () => { - if (typeof options.onSocketOpen === "function") { - let errorDescription; - try { - errorDescription = options.onSocketOpen(this._sock._websocket); - } catch (e) { - return this._fail("onSocketOpen callback failed: " + e.stack); - } - if (errorDescription) { - return this._fail("onSocketOpen callback failed: " + errorDescription); - } - } - if ((this._rfb_connection_state === 'connecting') && (this._rfb_init_state === '')) { this._rfb_init_state = 'ProtocolVersion'; Log.Debug("Starting VNC handshake"); + if (!this.dispatchEvent({type: 'webSocketOpen', ws: this._sock._websocket})) { + return this._fail("webSocketOpen event handler has prevented the further execution"); + } } else { this._fail("Unexpected server connection while " + this._rfb_connection_state); diff --git a/tests/test.rfb.js b/tests/test.rfb.js index 30a5b5ff..f70fbf42 100644 --- a/tests/test.rfb.js +++ b/tests/test.rfb.js @@ -1408,34 +1408,21 @@ describe('Remote Frame Buffer Protocol Client', function () { expect(client._sock).to.have.sent(new Uint8Array([0])); }); - it('should fail if onSocketOpen callback returns a failure description', function () { - const rfb = new RFB(container, 'wss://host:8675', { onSocketOpen: () => 'Something went wrong' }); - clock.tick(); - sinon.spy(rfb, "_fail"); - rfb._sock._websocket._open(); - expect(rfb._fail).to.have.been.calledOnce; + it('should fail if webSocketOpen event handler prevents execution', function () { + const client = make_rfb(); + client.addEventListener('webSocketOpen', (event) => { + event.defaultPrevented = true; + }); + sinon.spy(client, "_fail"); + client._sock._websocket._open(); + expect(client._fail).to.have.been.calledOnce; }); - it('should fail if onSocketOpen callback throws an exception', function () { - const rfb = new RFB(container, 'wss://host:8675', { onSocketOpen: () => { throw new Error(); } }); - clock.tick(); - sinon.spy(rfb, "_fail"); - rfb._sock._websocket._open(); - expect(rfb._fail).to.have.been.calledOnce; - }); - - it('should pass if onSocketOpen callback succeeds', function () { - const client = make_rfb('wss://host:8675', { onSocketOpen: (ws) => { - expect(!!ws).to.be.true; - }}); - client._rfb_connection_state = 'connecting'; - client._rfb_init_state = 'SecurityResult'; - client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0])); - expect(client._rfb_init_state).to.equal('ServerInitialisation'); - }); - - it('should pass if onSocketOpen callback is not a function', function () { - const client = make_rfb('wss://host:8675', { onSocketOpen: 123 }); + it('should pass if webSocketOpen event handler passes', function () { + const client = make_rfb(); + client.addEventListener('webSocketOpen', (event) => { + expect(!!event.ws).to.be.true; + }); client._rfb_connection_state = 'connecting'; client._rfb_init_state = 'SecurityResult'; client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));