From 1bac4cb1f2ce94bd60eab894b459b13485a8d38f Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Wed, 13 Feb 2013 09:53:09 +0100 Subject: [PATCH] JS: Prevent duplicate WebSocket objects. --- src/js/remote.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/js/remote.js b/src/js/remote.js index e1fd61c8d..47e468835 100644 --- a/src/js/remote.js +++ b/src/js/remote.js @@ -395,11 +395,13 @@ Remote.prototype.ledger_hash = function () { // Stop from open state. Remote.prototype._connect_stop = function () { - delete this.ws.onerror; - delete this.ws.onclose; + if (this.ws) { + delete this.ws.onerror; + delete this.ws.onclose; - this.ws.terminate(); - delete this.ws; + this.ws.close(); + delete this.ws; + } this._set_state('offline'); }; @@ -458,6 +460,12 @@ Remote.prototype._connect_start = function () { if (this.trace) console.log("remote: connect: %s", url); + // There should not be an active connection at this point, but if there is + // we will shut it down so we don't end up with a duplicate. + if (this.ws) { + this._connect_stop(); + } + var WebSocket = require('ws'); var ws = this.ws = new WebSocket(url);