简体   繁体   English

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

[英]Infinite polling with Angular 4 and Http observables

I'm trying to build an infinite polling in my Http service because I'm building a king of of dashborad who survey data's comming from a servor, here's my code who's almost working (in my console I see the Json comming but it doesn't reflect to my view...I would like to inject in my users: Observable 我正在尝试在我的Http服务中构建一个无限轮询,因为我正在建立一个dashborad之王来调查来自服务器的数据,这里是我的代码几乎正在工作(在我的控制台中,我看到Json正在进行,但它没有'反思我的看法...我想注入我的用户:可观察

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));
  }

您只需将您的polling observable分配给this.users

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM