编程语言
首页 > 编程语言> > javascript – 使用Angular 4和Http observable进行无限轮询

javascript – 使用Angular 4和Http 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()]);

标签:long-polling,angular-http,javascript,angular,rxjs5
来源: https://codeday.me/bug/20190828/1752159.html