繁体   English   中英

使用RxJS和angular 2 HTTP服务重新连接服务器

[英]Server reconnection using RxJS and angular 2 HTTP service

使用RxJS和Angular 2处理服务器重新连接的最佳方法是什么?

这是我到目前为止的内容:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/operator/takeUntil';
import 'rxjs/add/observable/throw';

@Injectable()
export class ConnectionService {

  // Stores the current status of the connection, false = not connected
  serviceOnline: BehaviorSubject<boolean> = new BehaviorSubject(false)

  constructor(
    private http: Http
  ) {
    this.serviceOnline.asObservable().subscribe((online) => {
      console.log("Service online: " + JSON.stringify(online))
    })
    setTimeout( () => this.setServiceOffline(), 2000 );
  }

  setServiceOffline() {
    console.log("Setting service offline");
    this.serviceOnline.next(false);
    this.testConnection().delay(2000).repeat().takeUntil(this.serviceOnline).subscribe();
  }

  testConnection(): Observable<boolean> {
    console.log("Testing connection");
    return new Observable(observer => {
      this.http.get(`http://endpoint/ping`)
        .timeout(5000)
        .catch((error: any) => Observable.throw(error))
        .subscribe(() => {
          console.log("Connection successful");
          if(this.serviceOnline.getValue() == false)
            this.serviceOnline.next(true);
          observer.next(true);
          observer.complete();
        }, (error) => {
          console.log("Connection failed");
          if(this.serviceOnline.getValue() == true)
            this.serviceOnline.next(false);
          observer.next(false);
          observer.complete();
        });
    })
  }
}

结果在控制台中:

Service online: false
Setting service offline
Service online: false
Testing connection
Connection failed

如果我替换此行。

this.testConnection().delay(2000).repeat().takeUntil(this.serviceOnline).subscribe();

this.testConnection().delay(2000).repeat().take(5).subscribe();

我确实得到了重复:

Service online: false
Setting service offline
Service online: false
Testing connection
Connection failed
Connection failed
Connection failed
Connection failed
Connection failed

文档中

public takeUntil(通知者:可观察):可观察

发出由源Observable发出的值,直到通知者Observable发出一个值。

仅当服务器状态发生更改时,才会更新serviceOnline BehaviorSubject。 在这行之后...

this.serviceOnline.next(false);

... serviceOnline仅在http服务返回成功响应并且当前在线状态为false(断开连接)时发出另一个值:

if(this.serviceOnline.getValue() == false)
   this.serviceOnline.next(true);

因此takeUntil应该起作用。 我在这里误解/错过了什么?

我是RxJS的新手-改进此代码中实现的其他任何指针也将不胜感激...

这是一个Plnkr: http ://plnkr.co/edit/WUKxH6nFxc8LMKcxDPql?p=info

RxJS 5.4版。 Angular版本4。


将serviceOnline返回为observable不能解决问题:this.testConnection()。delay(2000).repeat()。takeUntil(this.serviceOnline.asObservable())。subscribe();

嗯,您是否尝试过这样做?:

this.testConnection().delay(2000).repeat().takeUntil(this.serviceOnline.asObservable()).subscribe();

我认为出现问题是因为this.serviceOnline在订阅时始终发出最后一个值,因为它是BehaviorSubject的实例。 当您将其作为可观察的通知者传递给takeUntil运算符时, takeUntil订阅并立即收到一条通知,该通知会导致立即完成所生成的可观察对象。 请注意,notifier observable发出truefalse takeUntiltakeUntil在接收到任何值时完成。

请尝试.takeUntil(this.serviceOnline.filter(online => online === true))

暂无
暂无

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

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