Giter Site home page Giter Site logo

duplex's Introduction

duplex

NPM version

An optionally-secure, full-duplex TCP command server and client.

Server

// An insecure CommandServer (`Server` from `node:net`)
const server = new CommandServer({
  host: "localhost",
  port: 3351,
  secure: false,
});

// A secure CommandServer (`Server` from `node:tls`)
// https://nodejs.org/api/tls.html#new-tlstlssocketsocket-options
const server = new CommandServer({
  host: "localhost",
  port: 3351,
  secure: true,
  key: fs.readFileSync("certs/server/server.key"),
  cert: fs.readFileSync("certs/server/server.crt"),
  ca: fs.readFileSync("certs/server/ca.crt"),
  requestCert: true, 
});

// -------------------
// Defining a command handler
server.command(0, async (payload: any, connection: Connection) => {
  return { ok: "OK" };
});

Client

// An insecure client (`Socket` from `node:net`)
const client = new CommandClient({
  host: "localhost",
  port: 3351,
  secure: false,
});

// A secure client (`TLSSocket` from `node:tls`)
const client = new CommandClient({
  host: "localhost",
  port: 3351,
  secure: true,
  key: fs.readFileSync("certs/client/client.key"),
  cert: fs.readFileSync("certs/client/client.crt"),
  ca: fs.readFileSync("certs/ca/ca.crt"),
});

// -------------------
// Awaiting the response
try {
  const response = await client.command(0, { some: "payload" }, 1000);
  //                             command^  ^payload             ^expiration
  // response: { ok: "OK" };
} catch (error) {
  console.error(error);
}

// ...or receiving the response in a callback
const callback = (response: any, error: CodeError) => {
  if (error) {
    console.error(error.code);
    return;
  }

  // response is { ok: "OK" }
};

// Sending a command to the server
client.command(0, { some: "payload" }, 1000, callback);

duplex's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.