NodeJs中TCP粘包、分包解決方案讼积!

NodeJs中TCP粘包扛门、分包解決方案!

最新更新請(qǐng)查看github項(xiàng)目
配置介紹

  1. 本類庫(kù)提供對(duì)TCP粘包處理的解決方案
  2. 本庫(kù)默認(rèn)緩沖512個(gè)字節(jié)嚷闭,當(dāng)接收數(shù)據(jù)超過(guò)512字節(jié)攒岛,自動(dòng)以512倍數(shù)擴(kuò)大緩沖空間
  3. 本庫(kù)默認(rèn)采用包頭兩個(gè)字節(jié)表示包長(zhǎng)度
  4. 本庫(kù)默認(rèn)采用大端接模式接收數(shù)據(jù)
  5. 本庫(kù)可以配置自定義包頭長(zhǎng)度[后期迭代]
  6. 本頭可以配置大端小端讀取[后期迭代]

安裝

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);

測(cè)試截圖

源碼地址

喜歡的話請(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]}

源碼地址,喜歡的話請(qǐng)點(diǎn)star嗅榕,想訂閱點(diǎn)watch

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末顺饮,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子凌那,更是在濱河造成了極大的恐慌兼雄,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件帽蝶,死亡現(xiàn)場(chǎng)離奇詭異赦肋,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,347評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門佃乘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)囱井,“玉大人,你說(shuō)我怎么就攤上這事趣避∨优唬” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 157,435評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵鹅巍,是天一觀的道長(zhǎng)千扶。 經(jīng)常有香客問(wèn)我,道長(zhǎng)骆捧,這世上最難降的妖魔是什么澎羞? 我笑而不...
    開(kāi)封第一講書人閱讀 56,509評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮敛苇,結(jié)果婚禮上妆绞,老公的妹妹穿的比我還像新娘。我一直安慰自己枫攀,他們只是感情好括饶,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,611評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著来涨,像睡著了一般图焰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蹦掐,一...
    開(kāi)封第一講書人閱讀 49,837評(píng)論 1 290
  • 那天技羔,我揣著相機(jī)與錄音,去河邊找鬼卧抗。 笑死藤滥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的社裆。 我是一名探鬼主播拙绊,決...
    沈念sama閱讀 38,987評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼泳秀!你這毒婦竟也來(lái)了标沪?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 37,730評(píng)論 0 267
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤嗜傅,失蹤者是張志新(化名)和其女友劉穎金句,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體磺陡,經(jīng)...
    沈念sama閱讀 44,194評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡趴梢,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,525評(píng)論 2 327
  • 正文 我和宋清朗相戀三年漠畜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坞靶。...
    茶點(diǎn)故事閱讀 38,664評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡憔狞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出彰阴,到底是詐尸還是另有隱情瘾敢,我是刑警寧澤,帶...
    沈念sama閱讀 34,334評(píng)論 4 330
  • 正文 年R本政府宣布尿这,位于F島的核電站簇抵,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏射众。R本人自食惡果不足惜碟摆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,944評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望叨橱。 院中可真熱鬧典蜕,春花似錦、人聲如沸罗洗。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,764評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)伙菜。三九已至轩缤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間贩绕,已是汗流浹背火的。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,997評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留丧叽,地道東北人卫玖。 一個(gè)月前我還...
    沈念sama閱讀 46,389評(píng)論 2 360
  • 正文 我出身青樓公你,卻偏偏與公主長(zhǎng)得像踊淳,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子陕靠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,554評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容