持續更新,源碼地址,喜歡的話請點star,想訂閱點watchgit
npm i stickpackage
bufferSize:設置stick處理粘包的緩存空間
setReadIntBE(type) type:16 包頭長度爲2,short類型 setReadIntBE(type) type:32 包頭長度爲4,int類型
setReadIntLE(type) type:16 包頭長度爲2,short類型 setReadIntLE(type) type:32 包頭長度爲4,int類型
options.bufferSize: 設置用戶處理粘包的緩存空間 options.type:設置包頭爲16位或者32位模式(16|32) options.bigEndian: 設置大端、小端字節流模式,默認爲打斷模式,爲false時爲小端模式(true|false)
msgCenter.publish('123') => <Buffer 00 03 31 32 33> // 00 03 包長度 31 32 33 字符串123的ascii碼
msgHandleFun:業務上處理消息的函數 msgCenter.onMsgRecv(msg => { console.log(`recv data: ` + msg.toString()) ...do something })
// 默認client.js 採用 msgCenter.publish('...') 向服務端發消息 // 如下是服務端收到消息後,進行粘包處理 const MsgCenter = require('stickpackage').msgCenter const msgCenter = new MsgCenter() // server 監聽分包後的消息 msgCenter.onMsgRecv(data => { console.log(`recv data: ` + data.toString()) }) // 把 tcp server 監聽到的字節流,put到msgCenter中 msgCenter.putData(Buffer.from([0x00, 0x02, 0x31, 0x32, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34])) //=> recv data: 12 //=> recv data: 1234
// 默認client.js 採用 stick 配置的組包式向服務器發送消息 // 如下是服務端收到消息後,進行粘包處理 const Stick = require('stickpackage').stick; const stick = new Stick(1024).setReadIntBE('16') /* * 包含兩個數據包,10個字節,包頭爲short,兩個字節:[0x00, 0x02],[ 0x00, 0x04] * 數據包1:[0x00, 0x02, 0x66, 0x66] * 數據包2:[0x00, 0x04, 0x88, 0x02, 0x11, 0x11] */ const data = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11, 0x11]); /* 構造兩個buffer * data2_1包含: 第一個數據包的所有數據,第二個數據包的部分數據 * data2_2包含: 第二個數據包的剩餘數據 */ const data2_1 = Buffer.from([0x00, 0x00, 0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11]); const data2_2 = Buffer.from([0x11]); // 設置收到完整數據觸發器 stick.onData(function (data) { console.log('receive data,length:' + data.length); console.log(data) }); stick.putData(data); stick.putData(data2_1); stick.putData(data2_2); // 運行結果: // receive data,length:4 <Buffer 00 02 66 66> // receive data,length:6 <Buffer 00 04 88 02 11 11> // receive data,length:2 <Buffer 00 00> receive data, length:4 < Buffer 00 02 66 66> receive data, length:6< Buffer 00 04 88 02 11 11>
// Client.js const net = require('net') const stick = require('../../index') const msgCenter = new stick.msgCenter() const client = net.createConnection({ port: 8080, host: '127.0.0.1' }, function () { const msgBuffer = msgCenter.publish('username=123&password=1234567,qwe') client.write(msgBuffer) }) client.on('data', function (data) { console.log(data.toString()) }) client.on('end', function () { console.log('disconnect from server') })
// Server.js const net = require('net') const stick = require('../../index') const tcp_server = net.createServer(function (socket) { const msgCenter = new stick.msgCenter() socket.on('data', function (data) { msgCenter.putData(data) }) msgCenter.onMsgRecv(function (data) { console.log('recv data: ' + data.toString()) }) socket.on('close', function (error) { console.log('client disconnected') }) socket.on('error', function (error) { console.log(`error:客戶端異常斷開: ${error}`) }) }) tcp_server.on('error', function (err) { throw err }) tcp_server.listen(8080, function () { console.log('tcp_server listening on 8080') })
// Clinet.js const net = require('net') const client = net.createConnection({ port: 8080, host: '127.0.0.1' }, function () { const body = Buffer.from('username=123&password=1234567,qwe') // 寫入包頭 const headBuf = new Buffer(4) headBuf.writeUInt32BE(body.byteLength, 0) console.log('data length: ' + headBuf.readInt32BE()) // 發送包頭 client.write(headBuf) // 發送包內容 client.write(body) console.log('data body: ' + body.toString()) }) client.on('data', function (data) { console.log(data.toString()) }) client.on('end', function () { console.log('disconnect from server') })
// Server.js const net = require('net') const stick_package = require('../../index').stick const tcp_server = net.createServer(function (socket) { socket.stick = new stick_package(1024).setReadIntBE('32') socket.on('data', function (data) { socket.stick.putData(data) }) socket.stick.onData(function (data) { // 解析包頭長度 const head = new Buffer(4) data.copy(head, 0, 0, 4) // 解析數據包內容 const body = new Buffer(head.readInt32BE()) data.copy(body, 0, 4, head.readInt32BE()) console.log('data length: ' + head.readInt32BE()) console.log('body content: ' + body.toString()) }) socket.on('close', function (error) { console.log('client disconnected') }) socket.on('error', function (error) { console.log(`error:客戶端異常斷開: ${error}`) }) }) tcp_server.on('error', function (err) { throw err }) tcp_server.listen(8080, function () { console.log('tcp_server listening on 8080') })
MITgithub
Copyright (c) 2017-present, ximen (G.doe)npm
## [源碼地址,喜歡的話請點star,想訂閱點watch](https://github.com/lvgithub/stick)