[英]Too much data on Node.js socket?
I'm currently developing a system that gets data from a battery pack of an electric vehicle, stores it in a database and display it on a screen. 我目前正在开发一种系统,该系统可从电动汽车的电池组中获取数据,并将其存储在数据库中并在屏幕上显示。
So I have a Java - Application that reads the data from a hardware interface, interprets the values and sends it via Socket to a Node.js-Server. 因此,我有一个Java应用程序,该程序从硬件接口读取数据,解释值并通过套接字将其发送到Node.js服务器。 (Java App and Webserver are running on the same computer, so Url = localhost)
(Java App和Webserver在同一台计算机上运行,因此Url = localhost)
JAVA APP: JAVA APP:
s = new Socket();
s.connect(new InetSocketAddress(URL, PORT));
out = new PrintWriter( s.getOutputStream(), true);
for (DataEntry e : entries){
out.printf(e.toJson());
}
NODE: 节点:
sock.on('data', function(data) {
try{
var data = JSON.parse(data);
db.serialize(function(){
db.run("INSERT INTO DataEntry(value, MessageField, time) values(" + data.value + "," + data.messageFieldID + ", STRFTIME('%Y-%m-%d %H:%M:%f'))");
});
} catch(e){}
});
I get about 20 Messages per second from the hardware interface which are converted into 100 Json - Strings. 我从硬件接口每秒收到大约20条消息,这些消息被转换为100个Json-字符串。 So the webserver has to process one message in 10 ms, which I thought, is manageable.
因此,网络服务器必须在10毫秒内处理一条消息,我认为这是可管理的。 But here is the problem: If my entries - List (foreach loop) has more than 2 elements, the webserver gets 2 or more of the Json's in one message.
但这是问题所在:如果我的输入-列表(foreach循环)具有两个以上的元素,则Web服务器会在一条消息中获得Json的2个或更多。
So the first message was divided into 2 parts (ID 41,42) and was processed correctly. 因此,第一条消息被分为两部分(ID 41,42)并得到了正确处理。 But the second message was divided into 5 parts (ID 43-47), and the first 4 of them weren't sent alone, so only the last one was saved correctly.
但是第二条消息分为5个部分(ID 43-47),并且前4个消息不是单独发送的,因此只有最后一个消息被正确保存。
How can I ensure, that every Json is sent one another? 我如何确保每个Json都互相发送? Isn't there something like a buffer so that the socket.on method is called correctly for every message I send?
是否没有类似缓冲区的内容,以便对我发送的每条消息都正确调用socket.on方法?
I hope somebody of you can help me 我希望你们中有人可以帮助我
Thank you! 谢谢!
Benedikt :) Benedikt :)
TCP sockets are just streams and you shouldn't make any assumptions about how much of a "message" is contained in a single packet. TCP套接字只是流,您不应对单个数据包中包含多少“消息”做任何假设。
A simple solution to this is to terminate each message with a newline character since JSON cannot contain such a character. 一个简单的解决方案是用换行符终止每个消息,因为JSON不能包含这样的字符。 From there it's a simple matter of buffering data until you see a newline character.
从那里开始,很简单的就是缓冲数据,直到您看到换行符为止。 Then call
JSON.parse()
on your buffer. 然后在缓冲区上调用
JSON.parse()
。 For example: 例如:
var buf = '';
sock.on('data', function(data) {
buf += data;
var p;
// Use a while loop as it may be possible to have multiple
// messages buffered depending on chunk contents
while (~(p = buf.indexOf('\n'))) {
try {
var msg = JSON.parse(buf.slice(0, p));
} catch (ex) {
console.log('Bad JSON message: ' + ex);
}
buf = buf.slice(p + 1);
}
});
You will also need to change printf()
to println()
on the Java-side so that a newline character will be appended to each message. 您还需要在Java端将
printf()
更改为println()
,以便将换行符附加到每条消息中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.