如何在无休止的流请求中释放responseText的内存?

时间:2017-06-05 03:42:11

标签: javascript ajax angular asynchronous observable

我需要使用xmlHTTPRequest从无休止的动态JPEG数据流中获取数据,我听说responseText可以在不完成请求的情况下填充数据(它永远不会完成)。现在我遇到了问题。

我的请求是像这样与RxJS observable绑定。

  postActionGivenDataBinaryStream(url:string, data:string) : Observable<any>{

    this.binary_progress_call_accum = 0 ;
    this.previous_length = 0 ;

    return Observable.create((observer)=>{
        let xhr = new XMLHttpRequest() ;

        xhr.open('POST',url,true) ;
        xhr.setRequestHeader("Content-Type","application/json;charset=utf-8");
        //this way the binary data keeps populate at state 3
        xhr.overrideMimeType('text\/plain; charset=x-user-defined');

        xhr.onreadystatechange = () =>{
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    this.binary_progress_call_accum = 0 ;
                    this.previous_length = 0 ;
                    observer.complete();
                } else {
                    this.binary_progress_call_accum = 0 ;
                    this.previous_length = 0 ;
                    observer.error(xhr.response);
                }
            }
        }
        xhr.onprogress = (event)=>{
            let outstring:string = "";
            //should preordically emit the response text 

            if (this.binary_progress_call_accum > 1) {
                //just send the next piece of data
                let endCount = xhr.responseText.length ;

                //so here try to next a string

                for (let i = this.previous_length ; i < endCount; ++i ){
                    outstring += ("00" + (xhr.responseText.charCodeAt(i) & 0xFF).toString(16)).slice(-2) ;
                }

                observer.next(outstring) ;
                this.previous_length = endCount ;
            }
            else{
                for (let i = 0 ; i < xhr.responseText.length; ++i ){
                    outstring += ("00" + (xhr.responseText.charCodeAt(i) & 0xFF).toString(16)).slice(-2) ;
                }
                observer.next(outstring) ;
                this.previous_length = xhr.responseText.length ;
            }

            this.binary_progress_call_accum += 1;
        };

        xhr.send(data) ;

        //https://stackoverflow.com/a/38622923/921082
        //elegantly abort()
        return () => xhr.abort() ;
    }) ;

}

但是这段代码有严重的问题,我以30秒的间隔称之为可观察的,但是,它有时会卡住。当我的间隔可观察触发时,请求只会延迟30秒!没有做任何事情,控制台日志什么都没有。

我怀疑xhr.abort()需要很长时间才能完成,在完成之前,可观察的时间间隔会产生下一个请求,这将与之前的请求冲突,这会导致请求 - 响应延迟。那么有没有办法释放responseText的内存而不使用interval重新启动这样的xhr请求可观察?

1 个答案:

答案 0 :(得分:0)

这看起来很难,我实际上解决了它。

答案与我上面的代码无关,但代码调用此请求是可观察的。

我应该使用repeatWhen代替IntervalObservable。由于IntervalObservable并不关心请求是否实际完成,因此会在最后一次完成之前触发请求。而repeatWhen确保最后一个observable在下一次开始之前实际完成。

请查看此启示讨论https://github.com/ReactiveX/RxJava/issues/448