如何阻止Angular调用Spring webflux流远程关闭

问题描述 投票:1回答:1

我正在学习Spring webflux和Reactive Streams,并尝试了一个从mongoDB传输信息的服务。问题是当没有任何东西从MongoDB发送时,Spring关闭了请求。所以我真正想做的是:有一个Angular表显示我的mongodb中SPRING检索到的数据,每次更新/插入时,新数据自动进入Angular。

我发现的唯一方法是每隔XXXX毫秒调用我的角度服务。

有没有其他方法可以做到这一点?所以这是我的Spring Web服务代码:

@GetMapping(path="/stream/organisation",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Organisation> streamAll() {
    return organisationRepository.findAll();
}

我的Angular服务:

getOrganisationStream(): Observable<Array<Organisation>> {
    this.Organisations = [];
    return Observable.create((observer) => {
            const eventSource = new EventSource(this.url);
            eventSource.onmessage = (event) => {
                // tslint:disable-next-line:no-console
                console.debug('Received event: ', event);
                const json = JSON.parse(event.data);
                console.log(json);
                const org: Organisation = new Organisation();
                org.codeFase = json.codeFase;
                org.id = json.id;
                org.name = json.name;
                this.Organisations.push(org);
                console.log(this.Organisations.length);
                this.ngZone.run(() => {
                    observer.next(this.Organisations);
                });
            };
            eventSource.onerror = (error) => {
                if (eventSource.readyState === 0) {
                console.log('The stream has been closed by the server.');
                eventSource.close();
                observer.complete();
            } else {
                observer.error('EventSource error: ' + error);
            }
        };
    });
}

我的组件:

organisations: Observable<Organisation[]>;
constructor(private testService: TestService) {
}
ngOnInit(): void {
    this.organisations = this.testService.getOrganisationStream();
}

我的HTML:

   <div *ngFor="let org of organisations | async"> 
{{org.name}} {{org.codeFase}}
</div>
angular spring-webflux
1个回答
0
投票

你将需要使用qazxsw poi它是一个无限的流,它在外部关闭之前一直保持打开状态。

在您的存储库中执行以下操作:

tailable cursor

在客户端关闭连接时,在订阅丢弃时将关闭游标。

© www.soinside.com 2019 - 2024. All rights reserved.