]> git.wh0rd.org - chrome-ext/music-player-client.git/blobdiff - js/tcp-client.js
images: update small tile to try & pass CWS guidelines
[chrome-ext/music-player-client.git] / js / tcp-client.js
index 8d1db93eb9d12a74ac7ae9223dcecde2f81ccc7c..dd0c189dc62a7a711bd11c8cfbf649b854b5b0eb 100644 (file)
@@ -42,6 +42,7 @@ Author: Boris Smus (smus@chromium.org)
     // Socket.
     this.socketId = null;
     this.isConnected = false;
+    this.pollerId = null;
 
     log('initialized tcp client');
   }
@@ -49,7 +50,7 @@ Author: Boris Smus (smus@chromium.org)
   /**
    * Connects to the TCP socket, and creates an open socket.
    *
-   * @see http://developer.chrome.com/trunk/apps/socket.html#method-create
+   * @see http://developer.chrome.com/apps/socket.html#method-create
    * @param {Function} callback The function to call on connection
    */
   TcpClient.prototype.connect = function(callback) {
@@ -62,7 +63,7 @@ Author: Boris Smus (smus@chromium.org)
   /**
    * Sends a message down the wire to the remote side
    *
-   * @see http://developer.chrome.com/trunk/apps/socket.html#method-write
+   * @see http://developer.chrome.com/apps/socket.html#method-write
    * @param {String} msg The message to send
    * @param {Function} callback The function to call when the message has sent
    */
@@ -88,11 +89,15 @@ Author: Boris Smus (smus@chromium.org)
   /**
    * Disconnects from the remote side
    *
-   * @see http://developer.chrome.com/trunk/apps/socket.html#method-disconnect
+   * @see http://developer.chrome.com/apps/socket.html#method-disconnect
    */
   TcpClient.prototype.disconnect = function() {
     socket.disconnect(this.socketId);
-    this.isConnected = false;
+    if (this.isConnected) {
+      clearInterval(this.pollerId);
+      this.pollerId = null;
+      this.isConnected = false;
+    }
   };
 
   /**
@@ -101,14 +106,16 @@ Author: Boris Smus (smus@chromium.org)
    * we go ahead and connect to the remote side.
    *
    * @private
-   * @see http://developer.chrome.com/trunk/apps/socket.html#method-connect
+   * @see http://developer.chrome.com/apps/socket.html#method-connect
    * @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');
     }
@@ -123,12 +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.
-    setInterval(this._periodicallyRead.bind(this), 500);
+    clearInterval(this.pollerId);
+    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');
   };
@@ -136,9 +151,9 @@ Author: Boris Smus (smus@chromium.org)
   /**
    * Checks for new data to read from the socket
    *
-   * @see http://developer.chrome.com/trunk/apps/socket.html#method-read
+   * @see http://developer.chrome.com/apps/socket.html#method-read
    */
-  TcpClient.prototype._periodicallyRead = function() {
+  TcpClient.prototype.poll = function() {
     socket.read(this.socketId, null, this._onDataRead.bind(this));
   };
 
@@ -214,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;