[流媒体-Web]Fetch流式传输接收


fetch来异步接收

获取到result.valueuint8array bytes数组来处理

code

let url = 'http://com.a:8080/flv/stream';
// var url = 'LargeFile.txt';
let progress = 0;
let contentLength = 0;
fetch(url).then(function(response) {
    // get the size of the request via the headers of the response
    contentLength = response.headers.get('Content-Length');

    let pump = function(reader) {
        return reader.read().then(function(result) {
            // if we're done reading the stream, return
            if (result.done) {
                return;
            }

            // retrieve the multi-byte chunk of data
            // var chunk = result.value;
            // var text = '';
            console.log(result.value);
            // since the chunk can be multiple bytes, iterate through
            // each byte while skipping the byte order mark
            // (assuming UTF-8 with single-byte chars)
            // for (var i = 3; i < chunk.byteLength; i++) {
            //     text += String.fromCharCode(chunk[i]);
            // }

            // append the contents to the page
            // document.getElementById('content').innerHTML += text;
            // console.log(text);

            // report our current progress
            // progress += chunk.byteLength;
            // console.log(((progress / contentLength) * 100) + '%');

            // go to next chunk via recursion
            return pump(reader);
        });
    }

    // start reading the response stream
    return pump(response.body.getReader());
})
.catch(function(error) {
    console.log(error);
});
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容