Skip to content

Latest commit

 

History

History
496 lines (309 loc) · 24.4 KB

File metadata and controls

496 lines (309 loc) · 24.4 KB

API

Table of Contents

Client Functions

The core of the library for interacting with the server

createClient

Async function that creates a new krpc-node client

Parameters

  • options object? The options used to create the client, defaults to defaultCreateClientOptions
    • options.rpc object? The options used to create the web socket client for primary rpc calls to the server
      • options.rpc.protocol string The protocol to use to connect to the server. ws or wss. (optional, default ws)
      • options.rpc.host string The host address of the server. (optional, default "127.0.0.1")
      • options.rpc.port (string | number) The port number on which to connect to the server. (optional, default "50000")
      • options.rpc.wsProtocols (string | Array<string>)? WebSocket protocols to pass to the ws library.
      • options.rpc.wsOptions object? Additional connection options to pass to the ws library.
    • options.stream object? The options used to create the web socket client for stream rpc calls to the server
      • options.stream.protocol string The protocol to use to connect to the server. ws or wss. (optional, default ws)
      • options.stream.host string The host address of the server. (optional, default "127.0.0.1")
      • options.stream.port (string | number) The port number on which to connect to the server. (optional, default "50000")
      • options.stream.wsProtocols (string | Array<string>)? WebSocket protocols to pass to the ws library.
      • options.stream.wsOptions object? Additional connection options to pass to the ws library.
  • callback creationResultCallback? The function called once the client has been created.

Examples

const createClient = require('krpc-node');

      createClient(null, clientCreated);

      function clientCreated(err, client) {
          if (err) {
              throw err;
          }
          console.log('Connection Opened');
          client.send(client.services.krpc.getClients(), getClientsCompleted);
      }

      function getClientsCompleted(err, response) {
          if (err) {
              throw err;
          }
          expect(response.error).to.not.be.ok();
          expect(response.results.length).to.equal(1);
          let result = response.results[0];
          expect(result.error).to.not.be.ok();
          result.value.items.forEach(function(item) {
              expect(item).to.be.ok();
              console.log(item);
          });
      }

Returns Promise<client>

creationResultCallback

The callback that is called after attempting to create a new client

Type: Function

Parameters

  • error (null | Error) Lets the caller know if there was an error creating the client
  • client client The created client, ready to use

defaultCreateClientOptions

Default options used to create a client. Gets merged in with the options you provide

Examples

const defaultCreateClientOptions = {
     rpc: {
         protocol: 'ws',
         host: '127.0.0.1',
         port: '50000',
         wsProtocols: null,
         wsOptions: null
     },
     stream: {
         protocol: 'ws',
         host: '127.0.0.1',
         port: '50001',
         wsProtocols: null,
         wsOptions: null
     }
 };

client

Parameters

  • callbackStack Array<function> An ordered array of callback functions to call when responses are received.
  • decodeStack Array<function> An ordered array of decode functions to call when responses are received.
  • rpc object Contains items related to communicating directly with the server.
    • rpc.socket WebSocket The underlying websocket instance used for primary communications with the server.
    • rpc.emitter EventEmitter The event emitter for primary rpc connection to the server
    • rpc.on on Registers for one of the events for messages from the server [open, message, error, close].
  • send send Sends a one or more service call(s) to the server see send. The documentation for available service calls can be found in the services section of themain README.md
  • services object The collection of services that can be called.
  • encoders object The raw encoders that can be used to manually encode values.
  • decoders object The raw decoders that can be used to manually decode values.
  • streams object Will store the streams with the string representation of their unique id as the key. Values are of type stream
  • Establishes connectToStreamServer a separate connection to the stream server.
  • addStream addStream Adds a single call to the stream communication. Make sure you call connectToStreamServer fist.
  • removeStream removeStream Removes a single call from the stream communication. Make sure you call connectToStreamServer and of course have called addStream fist.
  • stream object Contains items related to communicating with the stream server.
    • stream.socket WebSocket The underlying websocket instance used to communicate with the server for stream information.
    • stream.emitter EventEmitter The emitter that handles events for stream information.
    • stream.on on Registers for one of the events for messages from the server sent via streams [open, message, error, close].
  • close close Disconnects the client (RPC & Stream) from the server.

send

Async function which sends one or more request(s) to the server

Parameters

Returns Promise<response> A promise which will resolve to the response from the server.

sendCallback

This callback that is called after attempting to send calls to the server

Type: Function

Parameters

  • error (null | Error) Lets the caller know if there was an error sending the calls to the server
  • response response The server response.

procedureCall

An object which represents a remote procedure call to execute on the server

Parameters

  • decode function A function used to decode the response when it is returned by the server
  • call object The actual call and any arguments to send to the server to execute.

on

Function that allows you to register for one of the events relating to the websocket [open, message, error, close].

Parameters

  • eventName The name of the event to register for
  • fn the function to execute when the event occurs

connectToStreamServer

Connects to the stream server in order to stream continuous updates

Parameters

  • clientId string The clientId returned via krpc.getClientId
  • connectToStreamServerCallback [callback] The callback to execute when done connecting

Returns Promise<void> A promise to resolve when done connecting

stream

An object representing a proceedure call that is being streamed from the server

Parameters

  • propertyPath string A unique name to represent the call
  • decode function The function used to decode responses for the server for this call
  • id Long A Long.js representation of the 64bit integer used to uniquely identify this stream on the server
  • value any? The last known value of the call

addStream

Adds an call to the continuous update stream.

Parameters

  • procedureCall procedureCall The call to add to the stream
  • propertyPath string A unique name to represent the call
  • callback addStreamCallback? the callback function to execute when the operation has ended

Returns Promise<stream> The stream that was added

addStreamCallback

This callback that is called after attempting to add a call to the stream

Type: Function

Parameters

  • error (null | Error) Lets the caller know if there was an error adding the call to the stream
  • The stream stream that was added

removeStream

Removes a call from the continuous update stream.

Parameters

  • propertyPath A unique name that represents the existing call that is being streamed
  • callback removeStreamCallback? The callback function to execute when the operation has ended

Returns Promise<void> If successful with resolve to nothing

removeStreamCallback

This callback that is called after attempting to remove a call from the stream

Type: Function

Parameters

  • error (null | Error) Lets the caller know if there was an error sending the calls to the server

close

Closes both the stream and rpc socket connection to the server. Should be called to free up resources and end the event loop.

Parameters

  • callback function? The callback to execute after closing.

Returns Promise<void>

Encoders

Functions to help with raw encoding of values to send to the server

encodeDouble

Takes in a value and encodes it as a double stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

Returns (ByteBuffer | void)

encodeFloat

Takes in a value and encodes it as a float stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

Returns (ByteBuffer | void)

encodeSInt32

Takes in a value and encodes it as a sInt32 stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

Returns (ByteBuffer | void)

encodeSInt64

Takes in a value and encodes it as a sInt64 stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

Returns (ByteBuffer | void)

encodeUInt32

Takes in a value and encodes it as a uInt32 stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

Returns (ByteBuffer | void)

encodeUInt64

Takes in a value and encodes it as a uInt64 stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

Returns (ByteBuffer | void)

encodeBool

Takes in a value and encodes it as a bool stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

Returns (ByteBuffer | void)

encodeString

Takes in a value and encodes it as a string stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

Returns (ByteBuffer | void)

encodeEnum

Returns a function that can be used to encode a string as the specific enum value.

Parameters

  • enumDefinition object The key-value enum object. Keys should be numbers, values should be strings.

Returns Function The function that will do the encoding.

encodeValueBasedOnEnum

Takes in a string value and using the provided enum definition encodes it as a sInt stored in a [ByteBuffer]https://www.npmjs.com/package/bytebuffer object for use with the protobufjs library.

Parameters

  • value The value to encode.

  • Throws Error If the provided value was not found in the enum definition

Returns (ByteBuffer | void)

Decoders

Functions to help with raw decoding of values to send to the server

decodeDouble

Takes in a node.js buffer object representing a double and decodes it.

Parameters

  • buffer ByteBuffer The buffer object

Returns (number | any)

decodeFloat

Takes in a node.js buffer object representing a float and decodes it.

Parameters

  • buffer ByteBuffer The buffer object

Returns (number | any)

decodeSInt32

Takes in a node.js buffer object representing a sInt32 and decodes it.

Parameters

  • buffer ByteBuffer The buffer object

Returns number

decodeSInt64

Takes in a node.js buffer object representing a sInt64 and decodes it.

Parameters

  • buffer ByteBuffer The buffer object

Returns (!Long | !{value: Long, length: number} | !Long | {value: !Long, length: number})

decodeUInt32

Takes in a node.js buffer object representing a uInt32 and decodes it.

Parameters

  • buffer ByteBuffer The buffer object

Returns ({value, length} | number | !{value: number, length: number})

decodeUInt64

Takes in a node.js buffer object representing a uInt64 and decodes it.

Parameters

  • buffer ByteBuffer The buffer object

Returns any

decodeBool

Takes in a node.js buffer object representing a bool and decodes it.

Parameters

  • buffer ByteBuffer The buffer object

Returns boolean

decodeString

Takes in a node.js buffer object representing a string and decodes it.

Parameters

  • buffer ByteBuffer The buffer object

Returns (string | !{string: string, length: number} | {string, length})

decodeEnum

Returns a function that can be used to decode a node.js buffer into an entry from the provided enum definition.

Parameters

  • enumDefinition object The key-value enum object. Keys should be numbers, values should be strings.

Returns Function The function that will do the decoding.

decodeBufferToEnumValue

Takes in a node.js buffer object representing a double and decodes it.

Parameters

  • buffer ByteBuffer The buffer object