13

我玩了 angular2 并在一段时间后卡住了。

使用http.get单个请求可以正常工作,但我想每 4 秒轮询一次实时数据,经过一段时间的修改并阅读了很多我最终得到的反应性东西:

Observable.timer(0,4000)
  .flatMap(
    () => this._http.get(this._url)
       .share()
       .map(this.extractData)
       .catch(this.handleError)
  )
  .share(); 

在-observable 发出请求结果后,是否有一种简单的方法可以开始(4 秒)间隔?http.get(或者我最终会进入可观察的地狱吗?)

我想要的时间表:

Time(s): 0 - - - - - 1 - - - - - 2 - - - - - 3 - - - - - 4 - - - - - 5 - - - - - 6
Action:  Request - - Response - - - - - - - - - - - - - - - - - - - -Request-... 
Wait:                | wait for 4 seconds -------------------------> |
4

4 回答 4

13

更新到 RxJS 6

import { timer } from 'rxjs';
import { concatMap, map, expand, catchError } from 'rxjs/operators';

pollData$ = this._http.get(this._url)
  .pipe(
    map(this.extractData),
    catchError(this.handleError)
  );

pollData$.pipe(
  expand(_ => timer(4000).pipe(concatMap(_ => pollData$)))
).subscribe();

我正在使用 RxJS 5,但我不确定 RxJS 4 等效运算符是什么。无论如何,这是我的 RxJS 5 解决方案,希望对您有所帮助:

var pollData = this._http.get(this._url)
            .map(this.extractData)
            .catch(this.handleError);
pollData.expand(
  () => Observable.timer(4000).concatMap(() => pollData)
).subscribe();

expand 操作符将发射数据并在每次发射时递归地启动一个新的 Observable

于 2016-06-24T07:53:06.227 回答
2

我自己设法做到了,唯一的缺点是http.get不能更容易地重复。

pollData(): Observable<any> {

  //Creating a subject
  var pollSubject = new Subject<any>();

  //Define the Function which subscribes our pollSubject to a new http.get observable (see _pollLiveData() below)
  var subscribeToNewRequestObservable = () => {
    this._pollLiveData()
      .subscribe(
      (res) => { pollSubject.next(res) }
      );
  };

  //Subscribe our "subscription-function" to custom subject (observable) with 4000ms of delay added
  pollSubject.delay(4000).subscribe(subscribeToNewRequestObservable);

  //Call the "subscription-function" to execute the first request
  subscribeToNewRequestObservable();

  //Return observable of our subject
  return pollSubject.asObservable();

}

private _pollLiveData() {

  var url = 'http://localhost:4711/poll/';

  return this._http.get(url)
    .map(
    (res) => { return res.json(); }
    );
};

这就是为什么您不能使用更直接的订阅的原因:

var subscribeToNewRequestObservable = () => {
    this._pollLiveData()
      .subscribe(pollSubject);
  };

-observable的完成http.get也将完成您的主题并防止它发出更多项目。


这仍然是一个冷的 observable,所以除非你订阅它,否则不会发出任何请求。

this._pollService.pollData().subscribe(
  (res) => { this.count = res.count; }
);
于 2016-06-24T06:06:32.747 回答
1

如果您希望轮询延迟取决于先前的请求完成状态,请对 Can Nguyen的答案进行小幅修改。

var pollData = () => request()   // make request
    .do(handler, errorHandler)   // handle response data or error
    .ignoreElements()            // ignore request progress notifications
    .materialize();              // wrap error/complete notif-ns into Notification

pollData()                            // get our Observable<Notification>...
  .expand(                            // ...and recursively map...
    (n) => Rx.Observable              // ...each Notification object...
      .timer(n.error ? 1000 : 5000)   // ...(with delay depending on previous completion status)...
      .concatMap(() => pollData()))   // ...to new Observable<Notification>
  .subscribe();

砰砰

或者:

var pollData = () => request()             // make request
    .last()                                // take last progress value
    .catch(() => Rx.Observable.of(null));  // replace error with null-value

pollData()
  .expand(
    (data) => Rx.Observable
      .timer(data ? 5000 : 1000)           // delay depends on a value
      .concatMap(() => pollData()))
  .subscribe((d) => {console.log(d);});    // can subscribe to the value stream at the end

砰砰

于 2017-08-29T11:11:52.203 回答
-1

如果这样更方便,您可以尝试使用间隔。呼叫subscribe使您Subscription可以在一段时间后取消轮询。

let observer = Observable.interval(1000 * 4);
let subscription = observer.subsscribe(x => {
    this._http.get(this._url)
     .share()
     .map(this.extractData)
     .catch(this.handleError)
});

....
// if you don't require to poll anymore..
subscription.unsubscribe();
于 2016-06-21T08:21:00.180 回答