The core of the library for interacting with the server
Async function that creates a new krpc-node client
Parameters
optionsobject? The options used to create the client, defaults to defaultCreateClientOptionsoptions.rpcobject? The options used to create the web socket client for primary rpc calls to the serveroptions.rpc.protocolstring The protocol to use to connect to the server.wsorwss. (optional, defaultws)options.rpc.hoststring 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.wsOptionsobject? Additional connection options to pass to the ws library.
options.streamobject? The options used to create the web socket client for stream rpc calls to the serveroptions.stream.protocolstring The protocol to use to connect to the server.wsorwss. (optional, defaultws)options.stream.hoststring 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.wsOptionsobject? Additional connection options to pass to the ws library.
callbackcreationResultCallback? 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);
});
}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 clientclientclient The created client, ready to use
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
}
};Parameters
callbackStackArray<function> An ordered array of callback functions to call when responses are received.decodeStackArray<function> An ordered array of decode functions to call when responses are received.rpcobject Contains items related to communicating directly with the server.sendsend 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.mdservicesobject The collection of services that can be called.services.krpcobject Main kRPC service, used by clients to interact with basic server functionality.See the KRPC service.services.spaceCenterobject Provides functionality to interact with Kerbal Space Program. This includes controlling the active vessel, managing its resources, planning maneuver nodes and auto-piloting.See the SpaceCenter service.services.drawingobject Provides functionality for drawing objects in the flight scene. For drawing and interacting with the user interface.See the Drawing service.services.uiobject Provides functionality for drawing and interacting with in-game user interface elements. For drawing 3D objects in the flight scene.See the UI service.services.infernalRoboticsobject This service provides functionality to interact with theInfernal Robotics mod.See the InfernalRobotics service.services.kerbalAlarmClockobject This service provides functionality to interact with theKerbal Alarm Clock mod.See the KerbalAlarmClock service.services.remoteTechobject This service provides functionality to interact withRemoteTech mod.See the RemoteTech service.
encodersobject The raw encoders that can be used to manually encode values.decodersobject The raw decoders that can be used to manually decode values.streamsobject Will store the streams with the string representation of their unique id as the key. Values are of type streamEstablishesconnectToStreamServer a separate connection to the stream server.addStreamaddStream Adds a single call to the stream communication. Make sure you call connectToStreamServer fist.removeStreamremoveStream Removes a single call from the stream communication. Make sure you call connectToStreamServer and of course have called addStream fist.streamobject Contains items related to communicating with the stream server.stream.socketWebSocket The underlying websocket instance used to communicate with the server for stream information.stream.emitterEventEmitter The emitter that handles events for stream information.stream.onon Registers for one of the events for messages from the server sent via streams [open, message, error, close].
closeclose Disconnects the client (RPC & Stream) from the server.
Async function which sends one or more request(s) to the server
Parameters
procedureCall(procedureCall | Array<procedureCall>) A list of rpc procedureCall to make on the serversendCallbacksendCallback? The optional callback to execute when the request is sent.
Returns Promise<response> A promise which will resolve to the response from the server.
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 serverresponseresponse The server response.
An object which represents a remote procedure call to execute on the server
Parameters
decodefunction A function used to decode the response when it is returned by the servercallobject The actual call and any arguments to send to the server to execute.
Function that allows you to register for one of the events relating to the websocket [open, message, error, close].
Parameters
eventNameThe name of the event to register forfnthe function to execute when the event occurs
Connects to the stream server in order to stream continuous updates
Parameters
clientIdstring The clientId returned via krpc.getClientIdconnectToStreamServerCallback[callback] The callback to execute when done connecting
Returns Promise<void> A promise to resolve when done connecting
An object representing a proceedure call that is being streamed from the server
Parameters
propertyPathstring A unique name to represent the calldecodefunction The function used to decode responses for the server for this callidLong A Long.js representation of the 64bit integer used to uniquely identify this stream on the servervalueany? The last known value of the call
Adds an call to the continuous update stream.
Parameters
procedureCallprocedureCall The call to add to the streampropertyPathstring A unique name to represent the callcallbackaddStreamCallback? the callback function to execute when the operation has ended
Returns Promise<stream> The stream that was added
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 streamThestream stream that was added
Removes a call from the continuous update stream.
Parameters
propertyPathA unique name that represents the existing call that is being streamedcallbackremoveStreamCallback? The callback function to execute when the operation has ended
Returns Promise<void> If successful with resolve to nothing
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
Closes both the stream and rpc socket connection to the server. Should be called to free up resources and end the event loop.
Parameters
callbackfunction? The callback to execute after closing.
Returns Promise<void>
Functions to help with raw encoding of values to send to the server
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
valueThe value to encode.
Returns (ByteBuffer | void)
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
valueThe value to encode.
Returns (ByteBuffer | void)
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
valueThe value to encode.
Returns (ByteBuffer | void)
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
valueThe value to encode.
Returns (ByteBuffer | void)
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
valueThe value to encode.
Returns (ByteBuffer | void)
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
valueThe value to encode.
Returns (ByteBuffer | void)
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
valueThe value to encode.
Returns (ByteBuffer | void)
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
valueThe value to encode.
Returns (ByteBuffer | void)
Returns a function that can be used to encode a string as the specific enum value.
Parameters
enumDefinitionobject The key-value enum object. Keys should be numbers, values should be strings.
Returns Function The function that will do the encoding.
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
-
valueThe value to encode. -
Throws Error If the provided value was not found in the enum definition
Returns (ByteBuffer | void)
Functions to help with raw decoding of values to send to the server
Takes in a node.js buffer object representing a double and decodes it.
Parameters
bufferByteBuffer The buffer object
Returns (number | any)
Takes in a node.js buffer object representing a float and decodes it.
Parameters
bufferByteBuffer The buffer object
Returns (number | any)
Takes in a node.js buffer object representing a sInt32 and decodes it.
Parameters
bufferByteBuffer The buffer object
Returns number
Takes in a node.js buffer object representing a sInt64 and decodes it.
Parameters
bufferByteBuffer The buffer object
Returns (!Long | !{value: Long, length: number} | !Long | {value: !Long, length: number})
Takes in a node.js buffer object representing a uInt32 and decodes it.
Parameters
bufferByteBuffer The buffer object
Returns ({value, length} | number | !{value: number, length: number})
Takes in a node.js buffer object representing a uInt64 and decodes it.
Parameters
bufferByteBuffer The buffer object
Returns any
Takes in a node.js buffer object representing a bool and decodes it.
Parameters
bufferByteBuffer The buffer object
Returns boolean
Takes in a node.js buffer object representing a string and decodes it.
Parameters
bufferByteBuffer The buffer object
Returns (string | !{string: string, length: number} | {string, length})
Returns a function that can be used to decode a node.js buffer into an entry from the provided enum definition.
Parameters
enumDefinitionobject The key-value enum object. Keys should be numbers, values should be strings.
Returns Function The function that will do the decoding.
Takes in a node.js buffer object representing a double and decodes it.
Parameters
bufferByteBuffer The buffer object