- Notifications
You must be signed in to change notification settings - Fork3
shardeum/lib-net
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A library for sending and receiving JSON messages over raw TCP sockets.
Fundamentally, this is just a networked event emitter. Under the hood, the library is using a UUID system to correlate messages to their response handler in order to facilitate the simulation of a request/response system. Hence, the library can only be used to send data to and from other servers using this library (for now).
You can install shardus net via npm:
npm i gitlab:shardus/shardus-net
If you're working on theshardus-net
package, make sure to run the commandnpm run build
manually to ensure both TypeScript and Rust files are generated.
For publishing a release to NPM, simply runnpm run release
command.
constport=1234constaddress='localhost'constsn=require('shardus-net')({ port, address})
import*asShardusNetfrom'shardus-net'constport=1234constaddress='localhost'constsn=ShardusNet.createNetwork({ port, address})
// If you want to send a one-way message, not expecting a response:constdestinationPort=53constdestinationAddress=8.8.8.8constdata={algebraic:'Yeah!'}constprotocol=awaitsn.send(destinationPort,destinationAddress,data)// Note: the promise returned by sn.send will resolve once the data has been// successfully sent, and has nothing to do with a response.// Now, if you _are_ expecting a response:constdestinationPort=53constdestinationAddress=8.8.8.8constdata={mathematical:'Alright!'}consttimeout=10000// how long to wait for the response (in ms)// Note: If the timeout is set to 0, the library will assume you're not waiting// for a response.constonResponse=data=>console.log(data)constonTimeout=()=>thrownewError('timed out :(')// You must be listening in order to receive responses, even if you don't// do anything with incoming data. In a normal use case, you will already// have a listener set up and do not need to execute this step.awaitsn.listen(()=>{})constprotocol=awaitsn.send(destinationPort,destinationAddress,data,timeout,onResponse,onTimeout)// Assuming the server you send to bounces back the data (see below for how to do this),// your console will log: "{ mathematical: 'Alright!' }"
constserver=awaitsn.listen((data,remote,protocol,respond)=>{// `remote` is an object with { address: <sender's address>, port: <origin port> }// Note: The port is of virtually no use -- it represents the port that data was// send _from_, and you cannot send anything back to that port.// `data` is of course whatever you've been sent. You've got mail!// `respond` is the function you can use to send data back.// In this example, we'll use respond to simply bounce back the data we were// given. This completes the example from `send` above.awaitrespond(data)})// You now have access to the "server" object, which contains the lower level// net server, if you need it.
// When you want to spin down your listener, simply call stopListening and pass in// the server object you were given when you started listening.awaitsn.stopListening(servers)
Contributions are very welcome! Everyone interacting in our codebases, issue trackers, and any other form of communication, including chat rooms and mailing lists, is expected to follow ourcode of conduct so we can all enjoy the effort we put into this project.
Special thanks to Aaron Sullivan (aasullivan1618@gmail.com) for the contributions that became the base for this library.You can find them here athttps://gitlab.com/Shardus/shardus-quic-net.git
About
Resources
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- TypeScript57.1%
- Rust41.7%
- JavaScript1.2%