使用Angular 4和Http可观察量进行无限轮询

时间:2017-05-18 14:00:51

标签: javascript angular long-polling rxjs5 angular-http

我试图在我的Http服务中建立一个无限轮询,因为我正在建立一个调查来自服务器的数据的dashborad之王,这里是我的代码&& #39;几乎正常工作(在我的控制台中,我看到Json即将到来,但它并没有反映到我的观点......我想注入我的用户:Observable

const usersURL = 'http://my.super.servor.php'

@Injectable()
export class UserService {

 users: Observable<User[]>

   constructor (public http:Http) {
     this.users = http.get(usersURL)
              genre mobile ou autre
              .map(res => [res.json()]);

        let i = this.users.subscribe(
          usersURL => console.log(usersURL),
         () => {}, // Here we catch up errors
         () => console.log("completed!") // Here we catch up if its completed
        )

    // Here's where I'm trying to do the polling every 5 secondes
    let tick$ = Observable.interval(5000);

    let response$ = 
      tick$
          .flatMap(() => http.get(usersURL))
          .map(res => [res.json()]);

    let stockPoller = response$.subscribe(res => console.log(res));
  }

1 个答案:

答案 0 :(得分:5)

您只需将轮询可观察量分配给this.users

this.users = tick$.flatMap(() => http.get(usersURL)).map(res => [res.json()]);