Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用节点通过套接字发送一个二进制值,并且想在数据前面发送一个 4 字节长度的值,以便接收者知道需要多少数据。
如何使用缓冲区完成此操作?我使用 Protobuf for node 首先构造一个缓冲区,然后我需要将大小附加到缓冲区的开头。
要追加到当前缓冲区的开头,最简单的方法是创建一个新缓冲区。
var buf = // Protobuf buffer // Create a 4-byte buffer with the length. var prefix = new Buffer(4); prefix.writeUint32LE(buf.length, 0); // Join them together as a new Buffer. var data = Buffer.concat([prefix, buf]);