为什么node.js将传入的数据分成块? [英] Why is node.js breaking incoming data into chunks?

查看:92
本文介绍了为什么node.js将传入的数据分成块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

node.js中的以下代码不会将所有传入的数据记录在括号内,而是将数据分解成块。例如,如果输入的数据是ABCDEF ... XYZ,则将数据记录为[ABC] [DEF] ... [XYZ]而不是[ABCDEF ... XYZ]。数据当然要大得多,字母只是一个例子。

The following code in node.js does not log all incoming data inside the brackets, rather, it breaks the data into chunks. So for example, if the incoming data is ABCDEF...XYZ it logs the data as [ABC][DEF]...[XYZ] rather than [ABCDEF...XYZ]. The data is much larger of course, the alphabet is just an example.

我应该如何写这个,以便所有传入的数据记录在括号内而不是零件?

How should I write this so that all incoming data is logged once inside the brackets and not in parts?

chatServer.on('connection', function(client) 
{
    client.on('data', function(data) 
    {
        console.log('[' + data.toString() + ']');
    })    
})


推荐答案

你的数据到达数据包,所以(在这种情况下)你应该将数据包连接到一个你定义的变量功能。

Well your data is arriving in packets, so (in this case) you should be concatenating the packets into a variable that you define outside the function.

buffer = '';

chatServer.on('connection', function(client) 
{
    client.on('data', function(data) 
    {
        buffer += data.toString();
    })    
});

console.log('[' + buffer + ']');

这篇关于为什么node.js将传入的数据分成块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆