Git Product home page Git Product logo

svc-facs-tcp's Introduction

svc-facs-tcp

This facility implements a promise based tcp client and a simple tcp rpc client with support of different encodings.

Config

This facility does not support config file and doesn't have any additional option beside default ones required by base facility.

API

fac.getClient

Initiates a tcp client instance for specific server.

Params:

  • host<string> - server host
  • port<int> - server port
  • encoding<string?> - optional, encoding, e.g. utf-8

Result:

  • TcpClient

Example:

const tcp = fac.getClient({
  host: '127.0.0.1',
  port: 7070,
  encoding: 'utf-8'
})

fac.getRPC

Initiates a RPC client instance for specific server.

Params:

  • tcp<TcpClient?> - tcp client instance, use either tcp or tcpOpts
  • tcpOpts<object> - new tcp client config
    • host<string> - server host
    • port<int> - server port
    • encoding<string?> - optional, encoding, e.g. utf-8
  • readStrategy<string?> - optional, response read strategy, default: TcpFacility.TCP_READ_STRATEGY.ON_DATA
  • json<boolean?> - optional, flag specifying that rpc requests/responses should be using json serialization, default: false
  • timeout<int?> - optional, rpc response timeout, default: 500ms
  • delay<int?>- optional, delay between two rpc requests, default: 50ms, useful when server closes connection on response

Result:

  • TcpRpcClient

Example:

const rpc1 = fac.getRPC({
  tcpOpts: {
    host: '127.0.0.1',
    port: 7070,
    encoding: 'utf-8'
  },
  readStrategy: TcpFacility.TCP_READ_STRATEGY.ON_DATA,
  json: true,
  timeout: 3000,
  delay: 50
})

// or with existing tcp client

const tcp2 = fac.getClient({
  host: '127.0.0.1',
  port: 7080,
  encoding: 'utf-8'
})
const rpc2 = fac.getRPC({
  tcp: tcp2,
  readStrategy: TcpFacility.TCP_READ_STRATEGY.ON_DATA,
  json: true,
  timeout: 3000,
  delay: 50
})

rpc.request

Performs a TCP request and waits for response.

Params:

  • payload<string|JSON|Buffer> - request payload

Result:

  • Promise<string|JSON|Buffer> - response from server

Example:

const rcp = fac.getRPC({ ... })
const res = await rpc.request({ ping: 1 })
console.log(res) // { pong: 2 }

tcp.write

Sends data to tcp server

Params:

  • data<string|Buffer> - data that will be sent to server

Reponse:

  • Promise<void>

Example:

const tcp = fac.getClient({ ... })
await tcp.write('{"ping":1}')

tcp.read

Waits and returns the data from the server

Params:

  • opts<Object>
    • strategy<int> - read strategy, 1 (TcpFacility.TCP_READ_STRATEGY.ON_DATA) - on('data') event, 2 (TcpFacility.TCP_READ_STRATEGY.ON_END) - on('end') event
    • timeout<int> - response timeout

Result:

  • Promise<string|Buffer>

Example:

const tcp = fac.getClient({ ... })
const res = await tcp.read({ strategy: TcpFacility.TCP_READ_STRATEGY.ON_DATA })
console.log(res) // '{"pong":2}'

svc-facs-tcp's People

Contributors

vigan-abd avatar prdn 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.