]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Unregister event listeners from websock.
authorJacob Swanner <jacob@jacobswanner.com>
Thu, 8 Jan 2015 20:25:55 +0000 (15:25 -0500)
committerJacob Swanner <jacob@jacobswanner.com>
Thu, 8 Jan 2015 20:25:55 +0000 (15:25 -0500)
Prevents possible memory and event notification leaks when tearing down
connection and reestablishing a new one.

include/rfb.js
include/websock.js
tests/test.rfb.js

index 59fd785d4f79a1fbae3681cfe9f71be414ce45ca..f461aff9d5a7734122dab5c4ddd6ed29ca2305b6 100644 (file)
@@ -197,6 +197,7 @@ var RFB;
             } else {
                 this._fail("Server disconnected" + msg);
             }
+            this._sock.off('close');
         }.bind(this));
         this._sock.on('error', function (e) {
             Util.Warn("WebSocket on-error event");
@@ -239,6 +240,9 @@ var RFB;
 
         disconnect: function () {
             this._updateState('disconnect', 'Disconnecting');
+            this._sock.off('error');
+            this._sock.off('message');
+            this._sock.off('open');
         },
 
         sendPassword: function (passwd) {
index 1b89a91f753261d27cd878043e0fa5bc59b8af80..cc82e5a27fd84938d358781fc6a318e245c39ba8 100644 (file)
@@ -200,6 +200,10 @@ function Websock() {
         },
 
         // Event Handlers
+        off: function (evt) {
+            this._eventHandlers[evt] = function () {};
+        },
+
         on: function (evt, handler) {
             this._eventHandlers[evt] = handler;
         },
index d80e3d523cff995576d685eb1259798c2cdc2b1d..d777a86096ee84ef7f41cca0b01f9dd5aee36cf7 100644 (file)
@@ -62,6 +62,24 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 expect(client._updateState).to.have.been.calledOnce;
                 expect(client._updateState).to.have.been.calledWith('disconnect');
             });
+
+            it('should unregister error event handler', function () {
+                sinon.spy(client._sock, 'off');
+                client.disconnect();
+                expect(client._sock.off).to.have.been.calledWith('error');
+            });
+
+            it('should unregister message event handler', function () {
+                sinon.spy(client._sock, 'off');
+                client.disconnect();
+                expect(client._sock.off).to.have.been.calledWith('message');
+            });
+
+            it('should unregister open event handler', function () {
+                sinon.spy(client._sock, 'off');
+                client.disconnect();
+                expect(client._sock.off).to.have.been.calledWith('open');
+            });
         });
 
         describe('#sendPassword', function () {
@@ -1710,6 +1728,14 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 expect(client._rfb_state).to.equal('failed');
             });
 
+            it('should unregister close event handler', function () {
+                sinon.spy(client._sock, 'off');
+                client.connect('host', 8675);
+                client._rfb_state = 'disconnect';
+                client._sock._websocket.close();
+                expect(client._sock.off).to.have.been.calledWith('close');
+            });
+
             // error events do nothing
         });
     });