Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
NotificationsYou must be signed in to change notification settings

shardeum/lib-net

 
 

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).

Installation

You can install shardus net via npm:

npm i gitlab:shardus/shardus-net

Local Development

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.

Usage

Javascript

constport=1234constaddress='localhost'constsn=require('shardus-net')({ port, address})

Typescript

import*asShardusNetfrom'shardus-net'constport=1234constaddress='localhost'constsn=ShardusNet.createNetwork({ port, address})

sn.send

// 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!' }"

sn.listen

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.

sn.stopListening

// 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)

Contributing

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

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript57.1%
  • Rust41.7%
  • JavaScript1.2%

[8]ページ先頭

©2009-2025 Movatter.jp