$ HTTP和流? [关闭]

问题描述 投票:-1回答:1

我想读的App.net public stream。然而,当我用一个简单$http.get()我只得到一个响应。

$http
  .get('https://alpha-api.app.net/stream/0/posts/stream/global')
  .success(function (results) {
    console.log('received results', results);
  })
;

如何连接到HTTP流在AngularJS? 有没有连这样或我将不得不使用类似的WebSockets?

javascript angularjs
1个回答
1
投票

$http是只有一个请求。对于长轮询,与$interval定期调用它。

function MyCtrl($scope, $timeout) {
  function poll() {
    $http.get('...').success(function(data) {
      $scope.posts = data;
    });
    $timeout(poll, 10000);
  }

  poll();
}

你也可以创建一个服务封装这个逻辑,但你需要的不是改变结果被更新固定的数据结构。喜欢的东西this

© www.soinside.com 2019 - 2024. All rights reserved.