在nodejs上流式传输时丢失数据

时间:2017-08-23 07:12:17

标签: javascript node.js streaming buffer

我正在尝试从客户端的服务器流式传输csv文件。但是,我在缓冲区之间丢失了数据。

客户端

let data = '';
let count = 0;
$(document).ready(() => {
    let xhr = new XMLHttpRequest();
    xhr.open('GET', '/test/api', true);
    xhr.onprogress = function () {
        console.log('------- new buffer -------');
        let incomingBuffer = xhr.responseText;
        let splitbuffer = incomingBuffer.split(',')
        console.log('firsts')
        console.log(splitbuffer[0])
        console.log(splitbuffer[1])

        console.log('lasts')
        console.log(splitbuffer[splitbuffer.length - 2])
        console.log(splitbuffer[splitbuffer.length - 1])

};
xhr.send()
});

服务器端

app.get('/test/api', (req, res) => {
    let stream = fs.createReadStream('./resources/onescsv.csv');
    stream = byline.createStream(stream);
    stream.pipe(res);
    stream.on('end', () => res.end());

});

我正在流式传输的数据是一个csv文件,该文件具有相同的10,000行,并且从1到32开始增加一列。因此每行有1,2,3,... 32。

当我运行此应用程序时,我得到了

------- new buffer -------
test.js:21 firsts
test.js:22 1
test.js:23 2
test.js:25 lasts
test.js:26 2
test.js:27 3
------- new buffer -------
test.js:21 firsts
test.js:22 1
test.js:23 2
test.js:25 lasts
test.js:26 31
test.js:27 32

第一个缓冲区在3处满,但新缓冲区再次从1开始 - 它应该从4开始并递增到32并从1到32循环,依此类推。所以我在4到32之间丢失数据。

这是为什么?我做错了什么?

修改

我实际上在客户端上显示了多个缓冲区,但无论它在最后一个缓冲区的哪里结束,它们都从1开始。

CSV内容

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32

以上只有10行,但重复到10,000行

删除署名并添加最多58列的客户端输出

------- new buffer -------
test.js:21 firsts
test.js:22 1
test.js:23 2
test.js:25 lasts
test.js:26 49
test.js:27 50
------- new buffer -------
test.js:21 firsts
test.js:22 1
test.js:23 2
test.js:25 lasts
test.js:26 57
test.js:27 58
------- new buffer -------
test.js:21 firsts
test.js:22 1
test.js:23 2
test.js:25 lasts
test.js:26 39
test.js:27 40
------- new buffer -------
test.js:21 firsts
test.js:22 1
test.js:23 2
test.js:25 lasts
test.js:26 57
test.js:27 58

第二个缓冲区文本的最后一部分的快照

9,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,
test.js:27 firsts
test.js:28 1
test.js:29 2
test.js:31 lasts
test.js:32 50

0 个答案:

没有答案