ash*_*evo 6 spring-boot jsonstream angular
Spring Boot 2 WebFlux在新版本中生成Json流
例如
@GetMapping(value = "stream", produces = APPLICATION_STREAM_JSON_VALUE)
public Flux<Data> stream() {
    return Flux.interval(Duration.ofSeconds(1)).map(Data::new);
}
Run Code Online (Sandbox Code Playgroud)
将每隔一秒产生一次新的数据
{"value":"1"}
{"value":"2"}
{"value":"3"}
{"value":"4"}
{"value":"5"}
{"value":"6"}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过angular 5 httpclient
findAll(): Observable<Data> {
   return this._http.get<Data>(this.url);
}
Run Code Online (Sandbox Code Playgroud)
但它不适合我,因为我想被反应它不会发送结果,因为它缓存结果,直到连接colsed
我想问一下在角度5中处理这个Json的最佳方法是什么
据我所知,仍然没有官方解决方案(19.08.2018),但是我找到了一些解决方法。的每一个方法HttpClient都有config参数,在这里你可以通过responseType和其他东西。我混合了下面的设置:
{observe: 'events', responseType: 'text', reportProgress: true}
Run Code Online (Sandbox Code Playgroud)
然后,您将收到给定类型的事件,范围在0到4之间。至少在我的情况type3中,有趣的内容在field中partialText,但是警告-在您的情况下,这些消息(在partialTextfield中)看起来像下面这样:
1条消息:
{"value":"1"}
Run Code Online (Sandbox Code Playgroud)
2条消息:
{"value":"1"}
{"value":"2"}
Run Code Online (Sandbox Code Playgroud)
3条消息
{"value":"1"}
{"value":"2"}
{"value":"3"}
Run Code Online (Sandbox Code Playgroud)
等等...所以,我像下面这样管理它:
method(url, /*if post - body,*/
      {observe: 'events', responseType: 'text', reportProgress: true})
      .pipe(
        filter(e => e.type === 3 && e.partialText),
        map(e => {
          const partials = e.partialText.trim().split('\n');
          return JSON.parse(partials.pop());
        })
      );
Run Code Online (Sandbox Code Playgroud)
        除了使用服务器发送事件或 WebSocket 之外,浏览器客户端无法使用 JSON 流 (application/stream+json)。
根据您所描述的要求和技术,WebSocket 更适合。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           1594 次  |  
        
|   最近记录:  |