【问题标题】:Pausing a non-flowing stream (delaying readable event)暂停非流动的流(延迟可读事件)
【发布时间】:2014-02-03 16:16:01
【问题描述】:

一方面,我有一个 http 请求(长轮询)。另一端是调度事件的“游戏服务器”。这些末端与对象模式下的双工非流动流捆绑在一起,并且该部分工作正常。

长轮询结束侦听readable 并通过反复调用stream.read 来排空流。然后关闭客户端的连接。

游戏服务器使用stream.write向客户端推送事件。

游戏中的一些事件实际上跨越了几个事件,这就是问题所在: 当游戏服务器一次添加多个事件(反复调用stream.write)时,第一次写入触发readable,长轮询被事件填充并关闭。这很不方便。

问题的本质是我不能静默readable,然后在我写完之后触发它。 所以我的问题是;我可以以某种方式“暂停”流并在之后恢复吗? 这个问题是否有已知的另一种解决方案?

我最好的办法是编写一系列事件,但我认为这在某种程度上是一种反模式。

这里有一些代码来说明我的问题:

var stream = require('stream');

var connection = stream.PassThrough({ objectMode: true });

var exhaust = function() {
    console.log('exhausting');
    var chunk;
    while ((chunk = connection.read()) !== null)
        console.log(chunk);

    console.log('exhausting end');
}

connection.on('readable', function(){
    console.log('Ready to read');
    exhaust();
});

for (var i = 0;i < 10;i++)
    connection.write({ test: true });

【问题讨论】:

    标签: node.js stream long-polling


    【解决方案1】:

    我最终向write 写了一系列事件,但我很期待这个即将推出的功能,这可能是missin 的解决方案:

    http://strongloop.com/strongblog/performance-node-js-v-0-12-whats-new/ http://nodejs.org/docs/v0.11.10/api/stream.html#stream_writable_cork

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      相关资源
      最近更新 更多