@hlhv/fsock v2.0.0
fsock
Simple socket framing module for HLHV.
How to use
First, you need to install the module,
npm install @hlhv/fsock
And require it.
// *.js
const FSock = require("@hlhv/fsock")
On both ends of a nodejs tcp connection, take the socket and pass it into a new FSock object. Then, set up data and error events:
const sock = net.createConnection ({
port: 4096
}, () => {
let fsock = new FSock(sock)
fsock.on ("data" (data) => {
console.log(data)
})
fsock.on ("error" (err) => {
console.log("ERR", err)
})
fsock.write(Buffer.from("this will get sent in an fsock frame!"))
})
It is important to set the data and error events of the FSock after you create
it, because these functions from the original sock will not carry over. Note -
if you assign a callback to an event name that is not special to FSock, it will
be assigned to the original socket instead (such as end
).
And that's it! You now have a very minimal setup for frames inside of a tcp
connection. It will go both ways - both sending and recieving are handled
through the write
method and the data
event (as long as both server and
client have set up an FSock object around their respective socket objects). If
you want some more detailed info about what this is or how it works, take a look
at the source code. It's only one file, around 60 lines long.