在 Angular 中重复调用 REST (5)
Call REST repeatedly in Angular (5)
我需要创建服务,该服务将在我的后端使用来自长轮询休息服务的数据。当我调用此服务时,后端会等到它有数据要发送或超时已过期并只发送空对象。
在 angular 我想要在应用程序完全加载时启动的服务,并且将 运行 在后台并将数据推送到我观察的主题。
可以告诉我最佳做法是什么,或者举例说明 angular 中的这项服务应该是什么样子?
这是我的尝试,但它也不在开始时调用 rest:
@Injectable()
export class PollingService {
private url: string;
constructor(private http: HttpClient, private fooService: FooService) {
this.url = 'http://localhost:8080/api/poll';
this.startPolling();
}
startPolling() {
this.http.get<any>(this.url)
.map(pollData => {
console.log('poll data', pollData);
this.fooService.mySubject.next(pollData);
if (pollData)
this.startPolling();
});
}
}
您没有从服务中获取任何数据的原因是您没有订阅它。
一个工作示例可能如下所示:
startPolling() {
this.http.get<any>('url').subscribe((pollData) => {
console.log('poll data', pollData);
this.fooService.mySubject.next(pollData);
if (pollData)
this.startPolling();
}, (error) => {
console.log(error);
// stop poll or use exponential backoff polling
})
然而,HTTP长轮询在现在服务器推送等概念中表现平平,实现它们的一种方法是通过web sockets。
您的 PollingService 应该只关注 HTTP 请求,让任何组件调用它。
@Injectable()
export class PollingService {
url: string;
constructor(private http: HttpClient, private fooService: FooService) {}
startPolling() {
this.url = 'http://localhost:8080/api/poll';
return this.http.get<any>(this.url);
}
}
你的组件:
export class YourComponent implement OnInit{
constructor(private pollingService : PollingService,
private fooService : FooService){}
theMethodYouWantToConsumeTheService(){
this.pollingService.startPolling().subscribe(
//If success
(pollData) => {
console.log('poll data', pollData);
this.fooService.mySubject.next(pollData);
},
//If error
(error)=>{
console.log(error);
}
);
}
}
我需要创建服务,该服务将在我的后端使用来自长轮询休息服务的数据。当我调用此服务时,后端会等到它有数据要发送或超时已过期并只发送空对象。
在 angular 我想要在应用程序完全加载时启动的服务,并且将 运行 在后台并将数据推送到我观察的主题。
可以告诉我最佳做法是什么,或者举例说明 angular 中的这项服务应该是什么样子?
这是我的尝试,但它也不在开始时调用 rest:
@Injectable()
export class PollingService {
private url: string;
constructor(private http: HttpClient, private fooService: FooService) {
this.url = 'http://localhost:8080/api/poll';
this.startPolling();
}
startPolling() {
this.http.get<any>(this.url)
.map(pollData => {
console.log('poll data', pollData);
this.fooService.mySubject.next(pollData);
if (pollData)
this.startPolling();
});
}
}
您没有从服务中获取任何数据的原因是您没有订阅它。
一个工作示例可能如下所示:
startPolling() {
this.http.get<any>('url').subscribe((pollData) => {
console.log('poll data', pollData);
this.fooService.mySubject.next(pollData);
if (pollData)
this.startPolling();
}, (error) => {
console.log(error);
// stop poll or use exponential backoff polling
})
然而,HTTP长轮询在现在服务器推送等概念中表现平平,实现它们的一种方法是通过web sockets。
您的 PollingService 应该只关注 HTTP 请求,让任何组件调用它。
@Injectable()
export class PollingService {
url: string;
constructor(private http: HttpClient, private fooService: FooService) {}
startPolling() {
this.url = 'http://localhost:8080/api/poll';
return this.http.get<any>(this.url);
}
}
你的组件:
export class YourComponent implement OnInit{
constructor(private pollingService : PollingService,
private fooService : FooService){}
theMethodYouWantToConsumeTheService(){
this.pollingService.startPolling().subscribe(
//If success
(pollData) => {
console.log('poll data', pollData);
this.fooService.mySubject.next(pollData);
},
//If error
(error)=>{
console.log(error);
}
);
}
}