src-example/client/js/app.js
/**
   * The application controller.
   *
   * Note that $http will be provided by httpOverWebSocket here.
   */
The application controller.
Note that $http will be provided by httpOverWebSocket here.
The application controller . Note that $http will be provided by httpOverWebSocket here .
function comparisonController ($scope, $http) {
    function requestStarted () {
      var newIndex = $scope.results.length;
      $scope.results[newIndex] = {
        index: newIndex + 1,
        startTime: Date.now(),
        completed: false
      };
      return newIndex;
    }
    function processResponse(index, success, response) {
      var result = $scope.results[index];
      $scope.firstResult = $scope.firstResult || result;
      result.endTime = Date.now();
      result.completed = true;
      result.success = success;
      result.elapsed = result.endTime - result.startTime;
      result.responseData = JSON.stringify(response.data);
      if (index === 0) {
        result.cached = false;
      } else if (result.responseData === $scope.firstResult.responseData) {
        result.cached = true;
      } else {
        result.cached = false;
      }
      if (!$scope.isRunning()) {
        $scope.runEnded();
      }
    }
    function sendRequest(url, callback) {
      var index = requestStarted();
      $http({
        cache: $scope.useCache,
        method: 'GET',
        url: url
      }).then(function (response) {
        processResponse(index, true, response);
        if (callback) {
          callback();
        }
      }, function (response) {
        processResponse(index, false, response);
        if (callback) {
          callback();
        }
      });
    }
    $scope.resultStatus = function (index) {
      var result = this.results[index];
      if (!result) {
        return '';
      }
      if (!result.completed) {
        return 'Running...';
      } else {
        if (result.success) {
          return 'Completed';
        } else {
          return 'Failed.';
        }
      }
    };
    $scope.isRunning = function () {
      if (!this.run.started || this.run.ended) {
        return false;
      }
      if (this.results.length < this.run.count) {
        return true;
      } else {
        return this.results.some(function (result) {
          return !result.completed;
        });
      }
    };
    $scope.runStarted = function (requestCount) {
      this.clear();
      this.run.count = requestCount;
      this.run.started = Date.now();
    };
    $scope.runEnded = function () {
      this.run.ended = Date.now();
      this.run.elapsed = this.run.ended - this.run.started;
    };
    $scope.clear = function () {
      this.run = {
        count: this.count,
        started: undefined,
        ended: undefined,
        elapsed: undefined,
        averagePerRequest: undefined
      };
      this.results = [];
      this.socketCallbacks = [];
      delete this.firstResult;
    };
    function getRunUrl(baseUrl) {
      return baseUrl + '/' + Date.now();
    }
    $scope.httpSeries = function () {
      if (this.isRunning()) {
        return;
      }
      var self = this;
      var runUrl = getRunUrl($scope.httpUrl);
      this.runStarted(this.count);
      async.timesSeries(this.count, function (index, asyncCallback) {
        sendRequest(runUrl, asyncCallback);
      }, function () {});
    };
    $scope.httpParallel = function () {
      if (this.isRunning()) {
        return;
      }
      var self = this;
      var runUrl = getRunUrl($scope.httpUrl);
      this.runStarted(this.count);
      async.times(this.count, function (index, asyncCallback) {
        sendRequest(runUrl);
        asyncCallback();
      }, function () {});
    };
    $scope.httpOverWebSocketSeries = function () {
      if (this.isRunning()) {
        return;
      }
      var self = this;
      var runUrl = getRunUrl($scope.httpOverWebSocketUrl);
      this.runStarted(this.count);
      async.timesSeries(this.count, function (index, asyncCallback) {
        sendRequest(runUrl, asyncCallback);
      }, function () {});
    };
    $scope.httpOverWebSocketParallel = function () {
      if (this.isRunning()) {
        return;
      }
      var self = this;
      var runUrl = getRunUrl($scope.httpOverWebSocketUrl);
      this.runStarted(this.count);
      async.times(this.count, function (index, asyncCallback) {
        sendRequest(runUrl);
        asyncCallback();
      }, function () {});
    };
    $scope.clear();
    $scope.count = 6;
    $scope.countOptions = [1, 6, 10, 20];
    $scope.useCache = false;
    $scope.useCacheOptions = [
      {
        label: 'without',
        value: false
      },
      {
        label: 'with',
        value: true
      }
    ];
    $scope.httpUrl = '/rest';
    $scope.httpOverWebSocketUrl = '/restOverWebSocket';
    $scope.title = 'Compare AngularJS ng.$http Versus httpOverWebSocket';
    $scope.$root.title = $scope.title;
  }
function comparisonController ( $scope , $http ) { function requestStarted ( ) { var newIndex = $scope . results . length ; $scope . results [ newIndex ] = { index : newIndex + 1 , startTime : Date . now ( ) , completed : false } ; return newIndex ; } function processResponse ( index , success , response ) { var result = $scope . results [ index ] ; $scope . firstResult = $scope . firstResult || result ; result . endTime = Date . now ( ) ; result . completed = true ; result . success = success ; result . elapsed = result . endTime - result . startTime ; result . responseData = JSON . stringify ( response . data ) ; if ( index === 0 ) { result . cached = false ; } else if ( result . responseData === $scope . firstResult . responseData ) { result . cached = true ; } else { result . cached = false ; } if ( ! $scope . isRunning ( ) ) { $scope . runEnded ( ) ; } } function sendRequest ( url , callback ) { var index = requestStarted ( ) ; $http ( { cache : $scope . useCache , method : 'GET' , url : url } ) . then ( function ( response ) { processResponse ( index , true , response ) ; if ( callback ) { callback ( ) ; } } , function ( response ) { processResponse ( index , false , response ) ; if ( callback ) { callback ( ) ; } } ) ; } $scope . resultStatus = function ( index ) { var result = this . results [ index ] ; if ( ! result ) { return '' ; } if ( ! result . completed ) { return 'Running...' ; } else { if ( result . success ) { return 'Completed' ; } else { return 'Failed.' ; } } } ; $scope . isRunning = function ( ) { if ( ! this . run . started || this . run . ended ) { return false ; } if ( this . results . length < this . run . count ) { return true ; } else { return this . results . some ( function ( result ) { return ! result . completed ; } ) ; } } ; $scope . runStarted = function ( requestCount ) { this . clear ( ) ; this . run . count = requestCount ; this . run . started = Date . now ( ) ; } ; $scope . runEnded = function ( ) { this . run . ended = Date . now ( ) ; this . run . elapsed = this . run . ended - this . run . started ; } ; $scope . clear = function ( ) { this . run = { count : this . count , started : undefined , ended : undefined , elapsed : undefined , averagePerRequest : undefined } ; this . results = [ ] ; this . socketCallbacks = [ ] ; delete this . firstResult ; } ; function getRunUrl ( baseUrl ) { return baseUrl + '/' + Date . now ( ) ; } $scope . httpSeries = function ( ) { if ( this . isRunning ( ) ) { return ; } var self = this ; var runUrl = getRunUrl ( $scope . httpUrl ) ; this . runStarted ( this . count ) ; async . timesSeries ( this . count , function ( index , asyncCallback ) { sendRequest ( runUrl , asyncCallback ) ; } , function ( ) { } ) ; } ; $scope . httpParallel = function ( ) { if ( this . isRunning ( ) ) { return ; } var self = this ; var runUrl = getRunUrl ( $scope . httpUrl ) ; this . runStarted ( this . count ) ; async . times ( this . count , function ( index , asyncCallback ) { sendRequest ( runUrl ) ; asyncCallback ( ) ; } , function ( ) { } ) ; } ; $scope . httpOverWebSocketSeries = function ( ) { if ( this . isRunning ( ) ) { return ; } var self = this ; var runUrl = getRunUrl ( $scope . httpOverWebSocketUrl ) ; this . runStarted ( this . count ) ; async . timesSeries ( this . count , function ( index , asyncCallback ) { sendRequest ( runUrl , asyncCallback ) ; } , function ( ) { } ) ; } ; $scope . httpOverWebSocketParallel = function ( ) { if ( this . isRunning ( ) ) { return ; } var self = this ; var runUrl = getRunUrl ( $scope . httpOverWebSocketUrl ) ; this . runStarted ( this . count ) ; async . times ( this . count , function ( index , asyncCallback ) { sendRequest ( runUrl ) ; asyncCallback ( ) ; } , function ( ) { } ) ; } ; $scope . clear ( ) ; $scope . count = 6 ; $scope . countOptions = [ 1 , 6 , 10 , 20 ] ; $scope . useCache = false ; $scope . useCacheOptions = [ { label : 'without' , value : false } , { label : 'with' , value : true } ] ; $scope . httpUrl = '/rest' ; $scope . httpOverWebSocketUrl = '/restOverWebSocket' ; $scope . title = 'Compare AngularJS ng.$http Versus httpOverWebSocket' ; $scope . $root . title = $scope . title ; }
The application controller.
The application controller .
/*----------------------------------------------------------------------
    Utility functions.
    ----------------------------------------------------------------------*/ /**
     * Note that a request is started.
     *
     * @return {number}
     *     Index of the new request.
     */ /**
     * Deal with the response from the server.
     */ // Complete the run if we're done. /**
     * Send an HTTP request to the server, which may go via WebSocket if the
     * URL matches.
     *
     * @param  {Function} [callback]
     *   Optional callback. Invoked when the request completes.
     */ /**
     * Return a status for the given result suitable for display.
     *
     * @param {number} index
     *   Index of the result in the results array.
     * @return {string}
     *   Display string for the status.
     */ /**
     * Is a test current running?
     */ /**
     * Note that a run started.
     *
     * @param {number} requestCount
     *   The number of requests to make.
     */ /**
     * Note that a run ended.
     */ /**
     * Clear the current test results.
     */ /**
     * Return a unique URL for this run.
     *
     * @param {string} baseUrl
     * @return {string}
     */ /*----------------------------------------------------------------------
    Test running functions.
    ----------------------------------------------------------------------*/ /**
     * Run a test against the server over HTTP in series.
     */ /**
     * Run a test against the server over HTTP in parallel.
     */ /**
     * Run a test against the server over Socket.IO in series.
     */ /**
     * Run a test against the server over Socket.IO in parallel.
     */ /*----------------------------------------------------------------------
    Initialization.
    ----------------------------------------------------------------------*/ // The normal limit on concurrent HTTP requests in Firefox. // Puts the title in the document head also.