Options
All
  • Public
  • Public/Protected
  • All
Menu

RPC over websocket, using the JSON-RPC 2.0 Specification http://www.jsonrpc.org/specification

A Cable establish an RPC communication over a websocket, there is no server/client role, the same cable can you can define methods on both sides of the cable and call the remote methods

example

// Client 1
const cable = new Cable(ws);
cable.register("ping", async () => {
  return "pong";
});
cable.notify("hello", {name:"client 1"});

// Client 2
const cable = new Cable(ws);
cable.register("hello", async ({name:string}) => {
  console.log(`${name} said hello`);
 });
try {
  const res = await cable.request("ping");
  assert.equal(res,"pong");
} catch(e) {
  if(e.code === Cable.SERVER_ERROR) {
    console.log("Implementation error on the server");
  }
  throw e;
}

Hierarchy

Implements

  • WebSocket

Index

Constructors

constructor

  • new Cable(ws: WebSocket): Cable

Properties

CLOSED

CLOSED: number = WebSocket.CLOSED

The connection is closed or couldn't be opened.

CLOSING

CLOSING: number = WebSocket.CLOSING

The connection is in the process of closing.

CONNECTING

CONNECTING: number = WebSocket.CONNECTING

The connection is not yet open.

OPEN

OPEN: number = WebSocket.OPEN

The connection is open and ready to communicate.

Protected _readyState

_readyState: number = WebSocket.CONNECTING

binaryType

binaryType: "blob" | "arraybuffer"

A string indicating the type of binary data being transmitted by the connection. This should be either "blob" if DOM Blob objects are being used or "arraybuffer" if ArrayBuffer objects are being used.

Protected closing

closing: boolean = false

Protected ws

ws: WebSocket

Static INTERNAL_ERROR

INTERNAL_ERROR: -32603 = -32603

Internal JSON-RPC error.

Static INVALID_PARAMS

INVALID_PARAMS: -32602 = -32602

Invalid method parameter(s).

Static INVALID_REQUEST

INVALID_REQUEST: -32600 = -32600

The JSON sent is not a valid Request object.

Static METHOD_NOT_FOUND

METHOD_NOT_FOUND: -32601 = -32601

The method does not exist / is not available.

Static PARSE_ERROR

PARSE_ERROR: -32700 = -32700

Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.

Static SERVER_ERROR

SERVER_ERROR: -32000 = -32000

Generic server-errors

Accessors

bufferedAmount

bufferedAmount:

The number of bytes of data that have been queued using calls to send() but not yet transmitted to the network. This value resets to zero once all queued data has been sent. This value does not reset to zero when the connection is closed; if you keep calling send(), this will continue to climb. Read only

extensions

extensions:

The extensions selected by the server. This is currently only the empty string or a list of extensions as negotiated by the connection.

onclose

onclose:

An event listener to be called when the WebSocket connection's readyState changes to CLOSED. The listener receives a CloseEvent named "close".

onerror

onerror:

An event listener to be called when an error occurs. This is a simple event named "error".

onmessage

onmessage:

An event listener to be called when a message is received from the server. The listener receives a MessageEvent named "message".

onopen

onopen:

An event listener to be called when the WebSocket connection's readyState changes to OPEN; this indicates that the connection is ready to send and receive data. The event is a simple one with the name "open".

protocol

protocol:

readyState

readyState:

The current state of the connection; this is one of the Ready state constants. Read only.

url

url:

The URL as resolved by the constructor. This is always an absolute URL. Read only.

Methods

addEventListener

  • addEventListener<K>(type: K, listener: function, useCapture?: boolean): void
  • Register an event handler of a specific event type on the EventTarget.

    Type parameters

    • K: keyof WebSocketEventMap

    Parameters

    • type: K
    • listener: function
        • (this: WebSocket, ev: WebSocketEventMap[K]): any
        • Parameters

          • this: WebSocket
          • ev: WebSocketEventMap[K]

          Returns any

    • Optional useCapture: boolean

    Returns void

close

  • close(code?: number, reason?: string): void
  • Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.

    Parameters

    • Default value code: number = 1000
    • Optional reason: string

    Returns void

dispatchEvent

  • dispatchEvent(evt: Event): boolean
  • Dispatch an event to this EventTarget.

    Parameters

    • evt: Event

    Returns boolean

    The return value is false if event is cancelable and at least one of the event handlers which handled this event called Event.preventDefault(). Otherwise it returns true.

Protected forwardEvents

  • forwardEvents<K>(list?: K[]): void

Protected getReadyState

  • getReadyState(): number

notify

  • notify(method: string, params?: any | any[]): void
  • Unlike the request, notify will not wait for the server to reply

    Parameters

    • method: string

      name

    • Optional params: any | any[]

    Returns void

register

  • register(name: string, method: function): void
  • Register a new method on the websocket

    Parameters

    • name: string
    • method: function

      name

        • (params: any): Promise<any>
        • handler

          Parameters

          • params: any

          Returns Promise<any>

    Returns void

removeEventListener

  • removeEventListener<K>(type: K, listener: function, useCapture?: boolean): void
  • Removes an event listener from the EventTarget.

    Type parameters

    • K: keyof WebSocketEventMap

    Parameters

    • type: K
    • listener: function
        • (this: WebSocket, ev: WebSocketEventMap[K]): any
        • Parameters

          • this: WebSocket
          • ev: WebSocketEventMap[K]

          Returns any

    • Optional useCapture: boolean

    Returns void

request

  • request(method: string, params?: any | any[], timeout?: number): Promise<Object>
  • Make a Rpc call

    Parameters

    • method: string

      name

    • Optional params: any | any[]
    • Optional timeout: number

    Returns Promise<Object>

send

  • send(data: USVString | ArrayBuffer | Blob | ArrayBufferView): void
  • Enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data. If the data can't be sent (for example, because it needs to be buffered but the buffer is full), t he socket is closed automatically.

    Parameters

    • data: USVString | ArrayBuffer | Blob | ArrayBufferView

      The data to send to the server. It may be one of the following types:

      • USVString A text string. The string is added to the buffer in UTF-8 format, and the value of bufferedAmount is increased by the number of bytes required to represent the UTF-8 string.
      • ArrayBuffer You can send the underlying binary data used by a typed array object; its binary data contents are queued in the buffer, increasing the value of bufferedAmount by the requisite number of bytes.
      • Blob Specifying a Blob enqueues the blob's raw data to be transmitted in a binary frame. The value of bufferedAmount is increased by the byte size of that raw data.
      • ArrayBufferView You can send any JavaScript typed array object as a binary frame; its binary data contents are queued in the buffer, increasing the value of bufferedAmount by the requisite number of bytes.

    Returns void

Protected stopForwardingEvents

  • stopForwardingEvents(): void

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc