gpt4 book ai didi

javascript - nodejs socket编程——发送数据长度

转载 作者:搜寻专家 更新时间:2023-11-01 00:40:02 25 4
gpt4 key购买 nike

我有一个服务器在特定端口上监听并期望命令如下 -

前四个字节应包含命令的长度,其余字节包含实际命令。例如:

如果我要发送的命令是{cmd:"EL",ptno:1234},我发送的前四个字节应该包含大端表示法中的数字 20 作为命令长度为 20,因为命令采用 UTF-8 格式。我发送的剩余字节将包含命令。

我想知道如何在 nodejs 中执行此操作。此外,当服务器发回数据时,我需要读取前四个字节并确定数据长度并相应地读取套接字输入流。请帮忙。

最佳答案

我做了一个完全符合您需要的库:https://www.npmjs.com/package/node-easysocket (除了在Little Endian中写了4个字节,但是很容易修复)

关于您的问题,我们开始:

对于发送消息,比接收容易得多,您只需要将消息转换为 ByteArray 并在前面添加一个包含 ByteArray + 4 大小的整数(4 字节 Big Endian):

var buffer = new Buffer("Hello World or a JSON String", "binary");

//create a buffer with +4 bytes
var consolidatedBuffer = new Buffer(4 + buffer.length);

//write at the beginning of the buffer, the total size
consolidatedBuffer.writeInt32BE(buffer.length, 0);

//Copy the message buffer to the consolidated buffer at position 4 (after the 4 bytes about the size)
buffer.copy(consolidatedBuffer, 4);

//Send the consolidated buffer
socket.write(consolidatedBuffer, function(err) {
if (err) console.log(err)
});

如果你想读取,它会稍微复杂一些,因为你有可能读取一个拼接成 block 的缓冲区。

示例:我的缓冲区大小为 10mb,但我的网络连接每秒可以传输约 100 字节,因此服务器将接收大量数据,您需要存储它们,直到它根据通知的长度完成必要的大小在前 4 个字节中。

Javascript 是一种动态语言,因此我可以在 socket 对象中创建一个运行时属性来存储收集到的 block :

socket.on('data', function(data) {
console.log("server bytes in:"+data.length);
receive(socket,data);
});


function receive(socket, data){
//Create a chunk prop if it does not exist
if(!socket.chunk){
socket.chunck = {
messageSize : 0,
buffer: new Buffer(0),
bufferStack: new Buffer(0)
};
}
//store the incoming data
socket.chunck.bufferStack = Buffer.concat([socket.chunck.bufferStack, data]);
//this is to check if you have a second message incoming in the tail of the first
var reCheck = false;
do {
reCheck = false;
//if message size == 0 you got a new message so read the message size (first 4 bytes)
if (socket.chunck.messageSize == 0 && socket.chunck.bufferStack.length >= 4) {
socket.chunck.messageSize = socket.chunck.bufferStack.readInt32BE(0);
}

//After read the message size (!= 0) and the bufferstack is completed and/or the incoming data contains more data (the next message)
if (socket.chunck.messageSize != 0 && socket.chunck.bufferStack.length >= socket.chunck.messageSize + 4) {
var buffer = socket.chunck.bufferStack.slice(4, socket.chunck.messageSize + 4);
socket.chunck.messageSize = 0;
socket.chunck.bufferStack = socket.chunck.bufferStack.slice(buffer.length + 4);
onMessage(socket, buffer);
//if the stack contains more data after read the entire message, maybe you got a new message, so it will verify the next 4 bytes and so on...
reCheck = socket.chunck.bufferStack.length > 0;
}
} while (reCheck);
}

function onMessage(socket, buffer){
console.log("message received from: "+socket+" with data:"+data.toString()+");
}

关于javascript - nodejs socket编程——发送数据长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38039362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com