【发布时间】:2017-05-21 07:38:07
【问题描述】:
我一直在挖掘 Angular 4 和 RxJS 的问题。这是我的代码,问题是,http.post 请求 Observable 没有向链的下一个 map() 发送任何内容。 POST 只会得到无休止的响应,它是 Motion JPEG 数据流。现在我必须处理数据,比如说每传输 500KB。我的问题是,如果 Observable 没有发出任何东西,我就无法对数据做任何事情。
那么如何让这种有角度的 HTTP 特性将传输的每个数据块发送到链的下一个?
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 ;
}
我这样调用上面的函数,post response是一个无穷无尽的二进制数据,我需要把它处理成字符串数组。
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();
【问题讨论】:
-
我不明白这个问题是关于什么的。你在哪里打电话
postActionGivenDataBinaryResponse?您期望在哪里获得价值但没有得到价值? -
@GünterZöchbauer,我为这个问题评论了我的代码。该功能只是一个POST。我希望数据会由块发出,但它现在永远不会发出,但调试显示数据传输没有问题。需要知道为什么。
-
@tomriddle_1234 您在文档中哪里看到 Angular Http.post 方法返回了一个 Observable,为响应中收到的每个字节发出一个事件?你为什么做出这样的假设?
-
@JBNizet,我想知道如何解决我的情况,你是说我不应该使用 Angular 的 HTTP 模块吗?我只需要接收到的每个数据块的 http 发出事件。怎么样?
-
我不清楚。但如果可能的话,我猜你将不得不使用较低级别的机制,直接使用 XMLHttpRequest。
标签: angular rxjs observable