无止境的HTTP Stream Observable除了传输的数据外不发射任何东西 [英] Endless HTTP Stream Observable not emitting anything but data transferred

查看:87
本文介绍了无止境的HTTP Stream Observable除了传输的数据外不发射任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Angular 4和RxJS的问题.这是我的代码,问题是http.post请求的Observable不会向链的下一个map()发送任何内容. POST只是得到了无尽的响应,它是Motion JPEG数据流.现在我必须处理数据,比如说每传输500KB.我的问题是,如果Observable没有发出任何数据,我将无法对数据做任何事情.

I've been digging an issue with Angular 4 and RxJS. Here is my code, the issue is, the http.post request Observable is not emitting anything to the next of the chain, the map(). The POST just get an endless response, it's a stream of Motion JPEG data. Now I have to deal with the data, say each 500KB transferred. My problem is that I cannot do anything with the data if the Observable not emitting anything.

那么如何使这种角度的HTTP功能将传输的每个数据块发送到链的下一个?

So how to make such angular HTTP feature emit to the next of the chain each chunk of data transferred?

postActionGivenDataBinaryResponse(url:string, data:string) : Observable<any>{
    let headers = new Headers ({
        'Content-Type' : 'application/json;charset=utf-8'
    }) ;
    let options = new RequestOptions({
        headers : headers,
        responseType: ResponseContentType.ArrayBuffer
    }) ;

    //return binary data as is no json
    return this.http.post(url, data, options) //suspecting here is not emitting anything due to unknown configuration issue.
                    .map(this.extractBinaryData)
                    .catch(this.handleError) ;
}

private extractBinaryData(res:Response){
    console.log('Dealing with data in the com service.') ; // this never runs
    return res ;
}

我这样调用上面的函数,后响应是一个无穷的二进制数据,我需要将其作为字符串数组进行处理.

I call the above function like this, the post response is an endless binary data, I need to process it as string array.

this.mySubscription = this.comService.postActionGivenDataBinaryResponse(myurl,mydata)
  .do(()=>{
              console.log('Observable Passed data.') ; //this never runs.
          })
  .bufferCount(128000)
  .map((data)=>{//process the data each 128kb chunk})
  .subscribe();

推荐答案

我通过MDN提供的技巧解决了此问题. 这是有关如何传输二进制数据的

I solved this issue by a trick that MDN provided. It's about how to transfer binary data

所以我写了自己的请求,而不是使用Angular的HTTP模块.

So I wrote my own request instead of using Angular's HTTP module.

并利用responseText技巧使请求永无止境.

And utilizing the responseText trick to keep the request never ending.

请求就像

function load_binary_resource(url) {
  var req = new XMLHttpRequest();
  req.open('GET', url, false);
  //XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  req.overrideMimeType('text\/plain; charset=x-user-defined');
  req.send(null);
  if (req.status != 200) return '';
  return req.responseText;
}

我只需要将其包装到一个可观察的对象中,然后数据将继续流传输并且永远不会停止,除非请求为abort().

I just need to wrap this into an observable, then data would keep streaming and never stop unless the request is abort().

这篇关于无止境的HTTP Stream Observable除了传输的数据外不发射任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆