Read/write stream messages prefixed 8, 16, 24 or 32 bit length.
npm i framed-stream
We have a server that handles connections like so:
const FramedStream = require('framed-stream')
const net = require('net')
const server = net.createServer().listen(3000)
server.on('connection', function (socket) {
const stream = new FramedStream(socket)
stream.on('data', (message) => console.log('client says:', message.toString()))
stream.on('end', () => stream.end())
stream.on('close', (message) => console.log('server stream is closed'))
})
Client does the same, also for example it sends two messages:
const client = net.connect(3000)
const stream = new FramedStream(client)
stream.on('data', (message) => console.log('server says:', message.toString()))
stream.on('end', () => stream.end())
stream.on('close', () => console.log('client stream is closed'))
stream.write('hello')
stream.write('world')
stream.end()
const stream = new FramedStream(rawStream, [options])Make a new framed stream.
Available options:
{
bits: 32
}
You can only set bits using the frameBits values documented below.
stream.rawStreamThe underlying raw stream.
stream.frameBitsIndicates the frame bits (8, 16, 24, or 32).
stream.frameBytesIndicates the frame bytes (1, 2, 3, or 4).
stream.maxMessageLengthIndicates the max message length (255, 65535, 16777215, or 4294967295).
MIT