NodeJs中TCP粘包扛门、分包解決方案!
最新更新請(qǐng)查看github項(xiàng)目
配置介紹
- 本類庫(kù)提供對(duì)TCP粘包處理的解決方案
- 本庫(kù)默認(rèn)緩沖512個(gè)字節(jié)嚷闭,當(dāng)接收數(shù)據(jù)超過(guò)512字節(jié)攒岛,自動(dòng)以512倍數(shù)擴(kuò)大緩沖空間
- 本庫(kù)默認(rèn)采用包頭兩個(gè)字節(jié)表示包長(zhǎng)度
- 本庫(kù)默認(rèn)采用大端接模式接收數(shù)據(jù)
- 本庫(kù)可以配置自定義包頭長(zhǎng)度[后期迭代]
- 本頭可以配置大端小端讀取[后期迭代]
安裝
npm i stickpackage
測(cè)試案例
var Stick = require('./stickPackage');
let stick = new Stick();
let bytes3 = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11, 0x11]);
let bytes4 = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11]);
let bytes5 = Buffer.from([0x11]);
// 512個(gè)字節(jié)包
let bytes6 = Buffer.from([0x01, 0xfe]);
let bytes7 = Buffer.alloc(510).fill(33);
// 513個(gè)字節(jié)包
let bytes8 = Buffer.from([0x01, 0xff]);
let bytes9 = Buffer.alloc(511).fill(33);
stick.onData(function (data) {
console.log('receive data,length:' + data.length);
console.log('receive data,contents:' + JSON.stringify(data));
// console.log('receive data,contents:');
});
// 傳入10個(gè)字節(jié),一個(gè)長(zhǎng)度為2,一個(gè)為4的數(shù)據(jù)包
console.log('log:傳入兩個(gè)包,一次Put[驗(yàn)證一次性Put數(shù)據(jù)包]');
stick.putData(bytes3);
// 傳入10個(gè)字節(jié),一個(gè)長(zhǎng)度為2,一個(gè)為4的數(shù)據(jù)包,分兩次Put
console.log('log:傳入兩個(gè)包,分兩次Put[驗(yàn)證分兩次Put數(shù)據(jù)包]');
stick.putData(bytes4);
stick.putData(bytes5);
console.log('log:傳入512個(gè)字節(jié)的數(shù)據(jù)包[驗(yàn)證緩沖全滿情況]');
stick.putData(bytes6);
stick.putData(bytes7);
console.log('log:傳入513個(gè)字節(jié)的數(shù)據(jù)包[驗(yàn)證緩沖擴(kuò)增情況]');
stick.putData(bytes8);
stick.putData(bytes9);
源碼地址
喜歡的話請(qǐng)點(diǎn)star,想訂閱點(diǎn)watch
案例分析
- 案例一:一次接受到兩個(gè)完整的數(shù)據(jù)包
測(cè)試數(shù)據(jù)包
let bytes3 = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11, 0x11]);
stick.putData(bytes3);
測(cè)試結(jié)果
[fly@fly Stickpackage]$ node stickTest.js
log:傳入兩個(gè)包,一次Put[驗(yàn)證一次性Put數(shù)據(jù)包]
receive data,length:4
receive data,contents:{"type":"Buffer","data":[0,2,102,102]}
receive data,length:6
receive data,contents:{"type":"Buffer","data":[0,4,136,2,17,17]}
- 案例二:接收第一包全包+第二包部分?jǐn)?shù)據(jù)胞锰,第二次接收第二包剩余數(shù)據(jù)
測(cè)試數(shù)據(jù)包
let bytes4 = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11]);
let bytes5 = Buffer.from([0x11]);
stick.putData(bytes4);
stick.putData(bytes5);
測(cè)試結(jié)果
log:傳入兩個(gè)包,分兩次Put[驗(yàn)證分兩次Put數(shù)據(jù)包]
receive data,length:4
receive data,contents:{"type":"Buffer","data":[0,2,102,102]}
receive data,length:6
receive data,contents:{"type":"Buffer","data":[0,4,136,2,17,17]}
- 案例三:數(shù)據(jù)剛好填滿緩沖
測(cè)試數(shù)據(jù)包
let bytes6 = Buffer.from([0x01, 0xfe]);
let bytes7 = Buffer.alloc(510).fill(33);
stick.putData(bytes7);
stick.putData(bytes7);
測(cè)試結(jié)果
傳入512個(gè)字節(jié)的數(shù)據(jù)包[驗(yàn)證緩沖全滿情況]
receive data,length:512
receive data,contents:{"type":"Buffer","data":[1,254,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33]}
- 案例四:數(shù)據(jù)超過(guò)滿緩沖空間灾锯,自動(dòng)擴(kuò)增緩沖
測(cè)試數(shù)據(jù)包
let bytes8 = Buffer.from([0x01, 0xff]);
let bytes9 = Buffer.alloc(511).fill(33);
stick.putData(bytes8);
stick.putData(bytes8);
測(cè)試結(jié)果
傳入513個(gè)字節(jié)的數(shù)據(jù)包[驗(yàn)證緩沖擴(kuò)增情況]
receive data,length:513
receive data,contents:{"type":"Buffer","data":[1,255,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33]}