You could try this PingService
- just set the url to your url.
@Injectable()
export class PingService {
pingStream: Subject<number> = new Subject<number>();
ping: number = 0;
url: string = "https://cors-test.appspot.com/test";
constructor(private _http: Http) {
Observable.interval(5000)
.subscribe((data) => {
let timeStart: number = performance.now();
this._http.get(this.url)
.subscribe((data) => {
let timeEnd: number = performance.now();
let ping: number = timeEnd - timeStart;
this.ping = ping;
this.pingStream.next(ping);
});
});
}
}
Use it in your component's constructor like so:
this._pingService.pingStream.subscribe(ping => {
this.ping = ping;
})
And in your template just add this where you want to show the current ping:
{{ping | number:'1.0-0'}}ms
Working Plunker for example usage