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)
});
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:"+buffer.toString()+");
}
1条答案
按热度按时间q3qa4bjr1#
我做了一个lib,它正好满足了你的需求:https://www.npmjs.com/package/node-easysocket(除了在小端写入4个字节,但很容易修复)
关于你的问题,我们开始:
发送消息比接收要容易得多,你只需要将消息转换为一个ByteArray,并在前面加上一个包含ByteArray + 4大小的整数(4字节Big Endian):
如果你想读取,它会有点复杂,因为你有可能读取拼接成块的缓冲区。
示例:我的缓冲区有10MB的大小,但我的网络连接每秒可以传输约100字节,因此服务器将接收大量数据,您将需要存储它们,直到它完成必要的大小,根据前4字节的长度通知。
Javascript是一种动态语言,所以我可以在socket对象中创建一个运行时属性来存储收集到的块: