node.js - readable.on ('end' ,...) 永远不会被解雇

标签 node.js stream

我正在尝试将一些音频流式传输到我的服务器,然后将其流式传输到用户指定的服务,用户将向我提供 someHostName,它有时不支持该类型的请求.

我的问题是,当它发生时 clientRequest.on('end',..) 永远不会被触发,我认为这是因为它被管道传输到 someHostReqsomeHostName 为“错误”时会变得困惑。

我的问题是:

即使流 clientRequest 管道有问题,我仍然可以触发 clientRequest.on('end',..) 吗?

如果不是:我如何“立即”检测到 someHostReq 发生了错误? someHostReq.on('error') 只在一段时间后才会启动。

代码:

    someHostName = 'somexample.com'

    function checkIfPaused(request){//every 1 second check .isPaused
        console.log(request.isPaused()+'>>>>');
        setTimeout(function(){checkIfPaused(request)},1000);
    }

    router.post('/', function (clientRequest, clientResponse) {
        clientRequest.on('data', function (chunk) {
            console.log('pushing data');
        });

        clientRequest.on('end', function () {//when done streaming audio
            console.log('im at the end');
        }); //end clientRequest.on('end',)

        options = {
            hostname: someHostName, method: 'POST', headers: {'Transfer-Encoding': 'chunked'}
        };

        var someHostReq = http.request(options, function(res){
            var data = ''
            someHostReq.on('data',function(chunk){data+=chunk;});
            someHostReq.on('end',function(){
                console.log('someHostReq.end is called');
            });
        });
        clientRequest.pipe(someHostReq);
        checkIfPaused(clientRequest);
    });

输出:

在主机名正确的情况下:

    pushing data
    .
    .
    pushing data
    false>>>
    pushing data
    .
    .
    pushing data
    pushing data
    false>>>
    pushing data
    .
    .
    pushing data
    console.log('im at the end');
    true>>>
    //continues to be true, that's fine

在主机名错误的情况下:

    pushing data
    .
    .
    pushing data
    false>>>>
    pushing data
    .
    .
    pushing data
    pushing data
    false>>>>
    pushing data
    .
    .
    pushing data
    true>>>>
    true>>>>
    true>>>>
    //it stays true and clientRequest.on('end') is never called
    //even tho the client is still streaming data, no more "pushing data" appears

如果您认为我的问题重复:

You can switch to flowing mode by doing any of the following:

Adding a 'data' event handler to listen for data.

Calling the resume() method to explicitly open the flow.

Calling the pipe() method to send the data to a Writable.

来源:https://nodejs.org/api/stream.html#stream_class_stream_readable

最佳答案

在主机名错误的情况下,缓冲区的行为似乎有些问题,如果目标流缓冲区已满(因为 someHost 没有获取发送的数据 block ),管道将不会继续读取原始流,因为管道自动管理流程。由于管道未读取原始流,因此您永远不会到达“结束”事件。

Is there anyway that I can still have clientRequest.on('end',..) fired even when the stream clientRequest pipes to has something wrong with it?

除非数据被完全消耗,否则“结束”事件不会触发。要使用暂停的流触发“结束”,您需要调用 resume()(首先从错误的主机名中取消管道,否则您将再次陷入缓冲区卡住)以再次将 Steam 设置为 flowMode 或 read() 到最后。

But how to detect when I should do any of the above?

someHostReq.on('error') 是很自然的地方,但如果启动时间太长:

首先尝试设置一个低超时请求(少于 someHostReq.on('error') 触发所需的时间,因为对您来说时间似乎太多了)request.setTimeout(timeout[, callback]) 并检查在正确的主机名时它是否不会失败。如果可行,只需使用 callbacktimeout 事件来检测服务器何时超时,并使用上述技术之一到达终点。

如果超时解决方案失败或不符合您的要求,您必须使用 clientRequest.on('data') 中的标志,clientRequest.on('end') 和/或 clienteRequest.isPaused 来猜测您何时被缓冲区卡住。当您认为自己卡住了时,只需应用上述技术之一即可到达流的末尾。幸运的是,与等待 someHostReq.on('error') 相比,检测缓冲区卡住花费的时间更少(可能有两个 request.isPaused() = true 没有达到 ' data' 事件足以确定您是否卡住了)。

How do I detect that something wrong happened with someHostReq "immediately"? someHostReq.on('error') doesn't fire up except after some time.

错误在触发时触发。您不能“立即”检测到它。 ¿为什么不在管道流之前发送一个证明信标请求来检查支持?某种:

“正在检查用户指定的服务...” 如果 OK -> 将用户请求流传输到服务 OR FAIL -> 通知用户错误的服务。

关于node.js - readable.on ('end' ,...) 永远不会被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34540095/

相关文章:

node.js - 为什么我在使用 Openshift 时遇到字体文件权限问题

node.js - 亚马逊 s3 和 Node js 视频流和缩略图?

stream - 如何使用 ffmpeg 创建流式音频文件?

C++ 菜鸟(流)...如何从字符串有空格的文件中读取字符串(例如,Tom Smith)?使用 getline 而不是 >> 时遇到问题

audio - 嵌入音频以节省带宽?要嵌入还是流式播放?

javascript - 获取node.js上文本字段的值

node.js - 使用 Nodejs Vows (BDD) 测试 Bluebird Promise

C++ 自定义流操纵器,可更改流中的下一项

php - 从 URL 下载文件到服务器

javascript - 如何测试包含未定义元素的数组的相等性?