簡體   English   中英

HTTP / 2:stream.end使用For-Loop

[英]HTTP/2: stream.end using For-Loop

如何在for-loop關閉流?

我通過讀取.json配置文件在循環中嘗試HTTP / 2資源push-stream 如果resurce.file,type只有一個資源resurce.file,type ,推送成功。 如果配置中有多個資源,則僅推送列出的第一個資源,並且不推送其余資源,客戶端永遠不會接收剩余文件,也不會完成解析。

我想只有第一個資源流結束,剩下的開放流沒有關閉。

我最好評論代碼應該正常運行:

// Verify if client can be pushed to:
if (res.push) {

  // Read what resources should be pushed via config.json5:
  for(var i = 0; i < confg.resource.length; i++) {

    // Compare what URL requested the resources & pass the relevant files:
    if (req.url == confg.resource[i].url) {

      // Request the resource(s) to stream:
      var ps  = fs.readFileSync('../build' + confg.resource[i].file)

          // Push the relevant resource stream(s):
        , stream = res.push(confg.resource[i].file
        , { // ..and set the content type;
            res: {'content-type': confg.resource[i].type},
          });

      // Close the resource stream(s):
      stream.end(ps, function() {
        // TODO(CelticParser): Remove for production ->v
        console.log("Pushed Sream");
      });
    }
  }
}

.config.json5:

resource: [ // HTTP/2 Push-Streams
  { file: '/js/theme.min.js',
     type: 'script'
  },
  { file: '/css/theme.min.css',
    type: 'style',
    url:  '/'
  }
]

使用上面的例子;

如果/js/theme.min.js列出/css/theme.min.css並且在配置中第二個列出/js/theme.min.js ,則瀏覽器將加載/js/theme.min.js ,而另一個文件將不加載,客戶端掛起( 不繼續解析 )。 如果交換資源的列表順序,則會發生相同的事情。 如果配置中只列出了一個文件,則一切都按預期工作。

任何幫助將不勝感激。

問題出在config.json5 您在傳遞文件之前檢查所請求的URL:

// Compare what URL requested the resources & pass the relevant files:
if (req.url == confg.resource[i].url) {
   ...
}

但是在你的json中只有一個項目可以通過測試,另一個項目缺少url屬性。 將您的配置更改為此(將url: '/'添加到第一個項目),它將起作用:

{
   resource : [
      { 
         file : '/js/theme.min.js',
         type : 'script',
         url  :  '/'
      },
      { 
         file : '/css/theme.min.css',
         type : 'style',
         url  :  '/'
      }
   ]
}

使用這個小應用程序進行測試,遵循http2服務器示例 ,以及您的服務器代碼。

server.js

var fs      = require("fs");
var path    = require("path");
var http2   = require("http2");
var config  = require("./config.json");

var options = {
    key  : fs.readFileSync("./localhost.key"),
    cert : fs.readFileSync("./localhost.crt")
};

//----------------------------------------------------
http2.createServer(options, function(req, res) {

    if (res.push) {

        config.resource.forEach(function(resource){

            if (req.url === resource.url) {

                var push = res.push(resource.file, { res: {"content-type": resource.type } });
                push.writeHead(200);
                fs.createReadStream(path.join("../build", resource.file)).pipe(push);
            }
        });
    }

    res.writeHead(200);
    res.end();

}).listen(8080);

config.json文件包含上述更正的配置。

輸出

Receiving pushed resource: ./js/bootstrap.min.js -> /Developer/so/tests/push-0
Receiving pushed resource: ./css/bootstrap.min.css -> /Developer/so/tests/push-1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM