Code Monkey home page Code Monkey logo

command-server's Introduction

command-server

Execute commands via a SSL encrypted connection.

CircleCI

Test Coverage Maintainability

Example

This example shows how a client and a server can be connected using self-signed certificates.

For some help on how to generate self-signed certificates see this comment.

const { CommandServer, CommandClient } = require('command-server')

/* ### Server ### */

const server = new CommandServer({
	host: 'localhost',
	port: 8090,

	key: fs.readFileSync('certs/server/server.key'),
	cert: fs.readFileSync('certs/server/server.crt'),
	ca: fs.readFileSync('certs/ca/ca.crt'),
	requestCert: true, // ask for a client cert
})

// Implement command 0.
server.command(0, async (payload) => {
	console.log('Server received:', payload)
	return 'result from the server'
})

/* ### Client ### */

const client = new CommandClient({
	host: 'localhost',
	port: 8090,

	key: fs.readFileSync('certs/client/client.key'),
	cert: fs.readFileSync('certs/client/client.crt'),
	ca: fs.readFileSync('certs/ca/ca.crt'),
})

// Execute the command 0.
client.command(0, 'message from the client', 1000).then((result) => {
	console.log('Client received::', result)
})

API

CommandClient

Extends the TokenClient from the token-server package.

CommandClient#command()

Executes a command on the server and then returns the result as a promise. The returned promise will be rejected when no response arrives before the expiresIn time has passed. The promise will also be rejected if an error occurs on the server during the execution of the command.

client.command(command, payload, expiresIn)
  • command <number> A number identifying the command you want to execute. The number must be between 0 and 254.
  • payload <any> A parameter that will be passed to the command. It may be of any value that can be stringified using JSON.stringify() so it must not be undefined.
  • expiresIn <number> Milliseconds until the command times out.

CommandServer

Extends the TokenServer from the token-server package.

CommandServer#command()

Executes a command on the server and then returns the result as a promise. The returned promise will be rejected when no response arrives before the expiresIn time has passed. The promise will also be rejected if an error occurs on the server during the execution of the command.

client.command(command, (payload, connection) => result)
  • command <number> A number identifying the command. The number must be between 0 and 254.
  • payload <any> The value that was passed as the payload parameter to the CommandClient#command().
  • connection <Connection> see here The connection that triggered the command.
  • result <any> A result that will be transmitted as response to the client. It may be of any value that can be stringified using JSON.stringify() so it must not be undefined.

command-server's People

Contributors

robojones avatar

Stargazers

 avatar  avatar

Watchers

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