StickPackage, NodeJs TCP StickPackage, subcontract solution!
Continue to update, source address, if you like, please click star, want to subscribe to watch configuration introduction
- Provides a solution for handling TCP sticky packets
- The default buffer size is 512 bytes. When receiving more than 512 bytes, the buffer size is automatically expanded by a factor of 512
- By default, the first two bytes of the packet represent the packet length
- By default, data is received in big-endian mode
- You can configure big-end and small-end reading
- You can customize the packet header length
Changes:
SetReadIntBE = “setReadIntLE”;
SetReadIntBE (16),setReadIntLE(16) * type:16 The packethead length is 2. The short type is setReadIntBE(32),setReadIntLE(32) * type:32 The packethead length is 4, and the type is intCopy the code
The installation
npm i stickpackage
Copy the code
Method of use
const Stick = require('./stickPackage'); const stick = new Stick( {bufferLength:1024} ).setReadIntBE(16); #const stick = new Stick( {bufferLength:1024} ).setReadIntBE(32); Stick. onData(function (data) {console.log('receive data,length:' + data.length); console.log('receive data,contents:' + JSON.stringify(data)); }); Construct a buffer containing two packets, 10 bytes # data1 [0x00, 0x02, 0x66, 0x66] 0x00 0x02 represents a packet length of 2 # data2 [0x00, 0x04, 0x88, 0x02, 0x11, Let bytes = buffer. from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11, 0x11]); Stick.putdata (bytes); Receive data,length:4 # receive data,contents:{"type":"Buffer","data":[0,2,102,102]} # receive data,length:6 # The receive data, the contents: {" type ":" Buffer ", "data" :,4,136,2,17,17 [0]}Copy the code
Source address, if you like, please click star, want to subscribe to watch