X-Git-Url: https://git.wh0rd.org/?p=chrome-ext%2Fmusic-player-client.git;a=blobdiff_plain;f=js%2Ftcp-client.js;h=dd0c189dc62a7a711bd11c8cfbf649b854b5b0eb;hp=55bdaafa658d55ed7e798076542fdb8dfb45a206;hb=HEAD;hpb=e7d6adb435d30041cb586158b52fd4fdd71cd088 diff --git a/js/tcp-client.js b/js/tcp-client.js index 55bdaaf..dd0c189 100644 --- a/js/tcp-client.js +++ b/js/tcp-client.js @@ -93,9 +93,11 @@ Author: Boris Smus (smus@chromium.org) */ TcpClient.prototype.disconnect = function() { socket.disconnect(this.socketId); - clearInterval(this.pollerId); - this.pollerId = null; - this.isConnected = false; + if (this.isConnected) { + clearInterval(this.pollerId); + this.pollerId = null; + this.isConnected = false; + } }; /** @@ -108,10 +110,12 @@ Author: Boris Smus (smus@chromium.org) * @param {Object} createInfo The socket details */ TcpClient.prototype._onCreate = function(createInfo) { + if (this.socketId !== null) { + socket.destroy(this.socketId); + } this.socketId = createInfo.socketId; if (this.socketId > 0) { socket.connect(this.socketId, this.host, this.port, this._onConnectComplete.bind(this)); - this.isConnected = true; } else { error('Unable to create socket'); } @@ -126,13 +130,20 @@ Author: Boris Smus (smus@chromium.org) * @param {Number} resultCode Indicates whether the connection was successful */ TcpClient.prototype._onConnectComplete = function(resultCode) { + log('resultCode: ' + resultCode); + + // XXX: Can this ever be positive ? + this.isConnected = (resultCode >= 0); + // Start polling for reads. clearInterval(this.pollerId); - this.pollerId = setInterval(this.poll.bind(this), 500); + if (this.isConnected) { + this.pollerId = setInterval(this.poll.bind(this), 500); + } if (this.callbacks.connect) { log('connect complete'); - this.callbacks.connect(); + this.callbacks.connect(resultCode); } log('onConnectComplete'); }; @@ -218,14 +229,14 @@ Author: Boris Smus (smus@chromium.org) * Wrapper function for logging */ function log(msg) { - //console.log('tcp-client: ', msg); + //console.log('tcp-client:', msg); } /** * Wrapper function for error logging */ function error(msg) { - console.error('tcp-client: ', msg); + console.error('tcp-client:', msg); } exports.TcpClient = TcpClient;