繁体   English   中英

带有nodejs(io.js),背压和消耗的流,`end`事件被调用两次

[英]stream with nodejs (io.js), backpressure and drain, `end` event is called twice

我有这个代码。 end事件被触发两次。 我不明白为什么。 有什么提示吗? 谢谢。

var chance = require('chance').Chance();

require('http').createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});

  function generateMore() {
    while(chance.bool({likelihood: 95})) {
      var shouldContinue = res.write(
        chance.string({length: (16 * 1024) - 1})
      );
      if(!shouldContinue) {
        console.log('Backpressure');
        return res.once('drain', generateMore);
      }
    }
    res.end('\nThe end...\n', function() {
      console.log('All data was sent'); // I see this log two times
    });
  }

  generateMore();

}).listen(8080, function () {
  console.log('Listening');
});

因为当您打开URL时,浏览器会尝试获取favicon.ico并将两个请求发送到您的服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM