Javascript 当一个大的响应到达时,将其分块阅读
我有一个node js服务器,它执行一个exe文件,以块的形式输出数据:Javascript 当一个大的响应到达时,将其分块阅读,javascript,node.js,xmlhttprequest,response,child-process,Javascript,Node.js,Xmlhttprequest,Response,Child Process,我有一个node js服务器,它执行一个exe文件,以块的形式输出数据: function requestHandler(request, response) { childproc = process.spawn("exefile"); childproc.stdout.on("data", (data) => { response.write(data); }); childproc.on("exit&q
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个请求)来获取所有块,似乎我缺少了一些酷而简单的方法