Class: APIRouter

APIRouter(configDataopt)

Transparently routes messages to/from available API communication interfaces.

Constructor

new APIRouter(configDataopt)

Creates an instance of APIRouter.
Parameters:
Name Type Attributes Default Description
configData Object <optional>
null Application configuration data, usually as loaded at startup.
Source:
See:

Extends

Members

(readonly) config

Properties:
Name Type Description
config Object Application configuration data, usually loaded at application startup (settings.json file).
Source:

connection

Properties:
Name Type Description
connection * A reference to the API connection, or null if no such connection exists or is invalid. The actual network connection handled by this object is rawConnection.
Source:

privateID

Properties:
Name Type Description
privateID String The privateID assigned to the session by the API connection. This value will be null if no session has been established.
Source:

(readonly) rawConnection

Properties:
Name Type Description
rawConnection * The raw network connection being used to communicate by the connection (e.g. a WebSocket, WebRTC, etc.)
Source:

serverToken

Properties:
Name Type Description
serverToken String Server-generated token required by the API connection.
Source:

userToken

Properties:
Name Type Description
userToken String Internally-generated user token required by the API connection.
Source:

Methods

addEventListener(type, listener, contextopt)

Registers a new event listener with the extending class instance.
Parameters:
Name Type Attributes Default Description
type String The event type to register.
listener function The listening function to invoke on the event.
context Object <optional>
null Unlike the traditional addEventListener parameter, this is the context or scope in which to invoke the listening function (since we can't use capture phases). If null, the listener is invoked in the context of the EventDispatcher or extending instance.
Inherited From:
Source:

(async) changePrivateID(newPrivateID) → {Promise}

Changes the private ID associated with the connection. The private ID is updated on the server and reflected in the privateID property.
Parameters:
Name Type Description
newPrivateID String The new private ID to set for this connection.
Source:
Returns:
The promise resolves with true if the private ID was successfully changed, otherwise it rejects with false.
Type
Promise

(async) connectAPI(connectInfo)

Establishes a connection to an API server.
Parameters:
Name Type Description
connectInfo Object An object containing information about the API server to connect to. The object must contain at least a type property.
Properties
Name Type Description
transport String Specifies the type of transport defined by the connectInfo object. This parameter is case-sensitive. Valid types include "ws" or "wss" for WebSocket Sessions, "wsst" for WebSocket Sessions tunnels, and "webrtc" for WebRTC connections.
Source:
Throws:
Thrown when the specified server could not be contacted or if there is a problem with the connectioInfo parameter.
Type
Error

(async) destroy()

Prepares the instance for destruction by closing any open transports, removing references and event listeners, and otherwise cleaning up.
Source:

getListeners(type) → {Array}

Returns all registered listeners for a specific event type.
Parameters:
Name Type Description
type String The event type to return registered listeners for.
Inherited From:
Source:
Returns:
A list of registered event listener objects for the specific event. Each object contains a listener function reference and an execution context reference.
Type
Array

(async) onConnectionClose(eventObj)

Event listener invoked when the connection dispatches a "close" event.
Parameters:
Name Type Description
eventObj Object A "close" event.
Source:
Fires:

removeEventListener(type, listener, contextopt)

Removes an event listener from the extending instance.
Parameters:
Name Type Attributes Default Description
type String The event type to remove the function from.
listener function The listening function to remove.
context Object <optional>
null The context or scope in which the listener exists.
Inherited From:
Source:

request(requestObj, responseMsgIDopt) → {Promise}

Sends an API request using the connection.
Parameters:
Name Type Attributes Default Description
requestObj Object The JSON-RPC 2.0 request to send.
responseMsgID String | Number <optional>
null The message ID if the response to match before the returned promise resolves. If null, the first returned response will resolve, even if it's not the expected response.
Source:
Returns:
An asynchronous promise that will resolve with a JSON-RPC 2.0 response. if responseMsgID is specified, only the response with the matching JSON-RPC id property will cause the promise to resolve,
Type
Promise

Events

close

The API connection has closed unexpectedly (the server terminated the connection wihout warning.)
Type:
  • Event
Source:

message

An API service message has been received on one of the supported transports being handled by this instance. Both "direct" and "message" types emit the same event (examine the data property to differentiate).
Type:
  • Event
Properties:
Name Type Description
data Object A native JSON-RPC 2.0 result or notification object.
transportType String The transport type through which the message was received.
transport Object A reference to the transport interface that initially handled the receipt of the message.
Source:

peerpid

The private ID for the API connection has changed.
Type:
  • Event
Properties:
Name Type Description
oldPrivateID String The previous private ID for the API connection.
newPrivateID String The new private ID for the API connection.
Source:

update

A server-originating "update" message has been received on one of the supported transports handled by this instance.
Type:
  • Event
Properties:
Name Type Description
data Object A native JSON-RPC 2.0 result or notification object.
transportType String The transport type through which the message was received.
transport Object A reference to the transport interface that initially handled the receipt of the message.
Source: