在到达时分块读取较大的响应

问题描述

我有一个节点js服务器,该服务器执行一个exe文件,该文件以大块形式输出数据:

function requestHandler(request,response)
{
  childproc = process.spawn("exefile");

  childproc.stdout.on("data",(data) =>
  {
    response.write(data);
  });

  childproc.on("exit",(code) =>
  {
    response.end();
  });
}

并且它被javascript客户端接收:

const xhr = new XMLHttpRequest();
...
xhr.send(...)
xhr.onreadystatechange = function ()
{
  if(xhr.readyState == XMLHttpRequest.LOADING)
  {
    // I can see this being called as and when the server is writing the data,// but ofcourse since the response has not ended,I get null when I try
    // xhr.response;
  }

  if (xhr.readyState == XMLHttpRequest.DONE)
  {
    response = xhr.response; // its an array buffer,by the way
  }
}

是否有一种不错的方式来获取数据,何时获取数据而无需等待整个过程结束?一种方法是在服务器端的每个块之后保持结束响应,但是我将不得不发起多个请求(多达10K个请求)来获取所有块,似乎我缺少了一些简单易用的方法。 / p>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)