美文网首页前端
[流媒体-Web]Fetch流式传输接收

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

作者: _小老虎_ | 来源:发表于2022-12-02 15:06 被阅读0次

    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);
    });
    

    相关文章

      网友评论

        本文标题:[流媒体-Web]Fetch流式传输接收

        本文链接:https://www.haomeiwen.com/subject/riksuktx.html