Project address: github.com/lvgithub/st… To test the demo, go to the example directory and run server.js
server
const net = require('net') const stick_package = require('.. /index') let 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) {let head = new Buffer(4) Data. copy(head, 0, 0, 4) // Let 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.stick.onError(function (error) { console.log('stick data error:' + error) }) socket.on('close', function (err) { console.log('client disconnected') }) }) tcp_server.on('error', function (err) { throw err }) tcp_server.listen(8080, function () { console.log('tcp_server listening on 8080') })Copy the code
client
const net = require('net') const client = net.createConnection({ port: 8080, host: '127.0.0.1'}, Function () {let body = Buffer. From ('username=123&password=1234567,qwe') headBuf.writeUInt32BE(body.byteLength, 0) console.log('data length: Write (headBuf) // Send packet content 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') })Copy the code