node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

krpc-node

krpc-node

A node.js client library for krpc. Allows you to send commands to Kerbal Space Program from node.

JavaScript to space via krpc!

NPM

Build status

Table of Contents

Client

Services:

  • SpaceCenter - Main api for controlling KSP.
  • UI - Api for interacting with user interface elements.
  • InfernalRobotics - Api for interacting with the Infernal Robotics mod.
  • KerbalAlarmClock - Api for interacting with the Kerbal Alarm Clock mod.
  • RemoteTech - Api for interacting with the Remote Tech mod.
  • Drawing - Api for for drawing objects in the flight scene.
  • KRPC - Api for interacting with the kRPC server.

Examples:

  • Examples - Some practical examples to get you started.

API

ClientConstructorFunction

Create a new krpc-node client

Parameters

  • options object The options used to create the client
    • options.protocol string ="ws" - The protocol to use to connect to the server. ws or wss.
    • options.host string ="127.0.0.1" - The host address of the server.
    • options.port (string | number) ="50000" - The port number on which to connect to the server.
    • options.wsProtocols [(string | Array<string>)] WebSocket protocols.
    • options.wsOptions [object] Additional connection options.
  • callback function The function called once the client has been created.

Callback Parameters

  • err Error The error object if there was a problem creating the client, otherwise null.
  • client Client The client to use for subsequent calls.

Examples

let util = require('util');
let Client = require('krpc-node');
let options = null;
Client(options, clientCreated);
 
function clientCreated(err, client) {
    if(err){
        throw err;
    }
    console.log(util.format('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);
    });
}

client

An instance of the Client class

Properties

  • 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 to communicate with the server.
    • rpc.emitter EventEmitter The emitter that handles events.
    • rpc.on function Registers for one of the events for messages from the server [open, message, error, close].
  • send function Sends one or more calls to the server to process
  • services object The collection of services that can be called. Each function within a service will return a procedureCall object.
    • services.drawing object Provides functionality for drawing objects in the flight scene. For drawing and interacting with the user interface, see the UI service.
    • services.infernalRobotics object This service provides functionality to interact with Infernal Robotics.
    • services.kerbalAlarmClock object This service provides functionality to interact with Kerbal Alarm Clock.
    • services.krpc object Main kRPC service, used by clients to interact with basic server functionality.
    • services.remoteTech object This service provides functionality to interact with RemoteTech.
    • services.spaceCenter object Provides functionality to interact with Kerbal Space Program. This includes controlling the active vessel, managing its resources, planning maneuver nodes and auto-piloting.
    • services.ui object Provides functionality for drawing and interacting with in-game user interface elements. For drawing 3D objects in the flight scene, see the Drawing service.
  • 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 The list of registered stream responses and how to decode them
  • streamState object The last known values of the result returned from the streams.
  • connectToStreamServer function Establishes a separate connection to the stream server.
  • addStream function Adds a single call to the stream communication. Make sure you call connectToStreamServer fist.
  • removeStream function 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.
    • stream.emitter EventEmitter The emitter that handles events.
    • stream.on function Registers for one of the events for messages from the server [open, message, error, close].

On

Registers for one of the events [open, message, error, close].

Parameters

  • eventName string The event to register for [open, message, error, close].
  • fn function The function to execute when an event happens

Send

Sends one or more calls to the server to process

Parameters

Callback Parameters

  • err Error The error object if there was a problem creating the client, otherwise null.
  • response Object The client response object.

procedureCall

A procedure call with a decode function an a procedure object to send to the server

Properties

  • decode function The function used to decode the response from the server.
  • call object The actual call + arguments to send to the server to execute.

addStream

Adds an call to the continuous update stream.

Parameters

  • call procedureCall One or more calls to send to the server.
  • propertyPath procedureCall The lodash set path to use to set the result of the stream call on client.streamState.
  • callback function The function called once the stream has been added.

Callback Parameters

  • err Error The error object if there was a problem creating the client, otherwise null.
  • stream Object The stream object.

removeStream

Removes a call from the continuous update stream.

Parameters

  • propertyPath procedureCall The lodash set path to used to set the result of the stream call on client.streamState.
  • callback function The function called once the stream has been added.

Callback Parameters

  • err Error The error object if there was a problem creating the client, otherwise null.

Practical Examples

Checkout the examples repository for some practical examples.