Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Client

RPC websocket client class which establishes the connection to the server. It inherits from browser's Websocket class, but for the connection you need to provide the unique client token in the constructor to let the server sends you method call using it. This class can register any method to call it by the server. And also can call any method on the server and receive response data.

The main parts of this class are:

  • register method to handle clients' calls
  • call method to execute clients' methods
example
import Client from '@noveo/dual-rpc-ws/client';
const client = new Client({
  token: 'id13',
  address: 'ws://192.168.0.80:8080',
  handshake: (connected) => {
    console.log('connected', connected);
    this.call(token, 'hi', {message: 'hello from client'});
  }
});
client.register('hi', (params, context) => {
  console.log('client hi, call id:', context.id);
  return Promise.resolve(`${token}, hello`);
});
client.addEventListener('close', () => {
  console.log('connection closed');
});

Hierarchy

Index

Constructors

constructor

Properties

CLOSED

CLOSED: number

CLOSING

CLOSING: number

CONNECTING

CONNECTING: number

OPEN

OPEN: number

binaryType

binaryType: BinaryType

Returns a string that indicates how binary data from the WebSocket object is exposed to scripts:

Can be set, to change how binary data is returned. The default is "blob".

bufferedAmount

bufferedAmount: number

Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network.

If the WebSocket connection is closed, this attribute's value will only increase with each call to the send() method. (The number does not reset to zero once the connection closes.)

extensions

extensions: string

Returns the extensions selected by the server, if any.

handshake

handshake: (connected: boolean) => void

Type declaration

    • (connected: boolean): void
    • Parameters

      • connected: boolean

      Returns void

Private methods

methods: Map<Name, (params: Record<string, any>, ctx: RPCContext) => Promise<JSONValue> | JSONValue | undefined>

onclose

onclose: ((this: WebSocket, ev: CloseEvent) => any) | null

onerror

onerror: ((this: WebSocket, ev: Event) => any) | null

onmessage

onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null

onopen

onopen: ((this: WebSocket, ev: Event) => any) | null

protocol

protocol: string

Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation.

readyState

readyState: number

Returns the state of the WebSocket object's connection. It can have the values described below.

Private requests

requests: Map<Id, Request>

url

url: string

Returns the URL that was used to establish the WebSocket connection.

Static WebSocket

WebSocket: { constructor: any; CLOSED: number; CLOSING: number; CONNECTING: number; OPEN: number; prototype: WebSocket }

Type declaration

  • constructor: function
    • new __type(url: string, protocols?: string | string[]): WebSocket
    • Parameters

      • url: string
      • Optional protocols: string | string[]

      Returns WebSocket

  • CLOSED: number
  • CLOSING: number
  • CONNECTING: number
  • OPEN: number
  • prototype: WebSocket

Methods

addEventListener

  • addEventListener<K>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void
  • addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void
  • Type parameters

    • K: keyof WebSocketEventMap

    Parameters

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

          Returns any

    • Optional options: boolean | AddEventListenerOptions

    Returns void

  • Parameters

    • type: string
    • listener: EventListenerOrEventListenerObject
    • Optional options: boolean | AddEventListenerOptions

    Returns void

call

  • call(method: string, params?: Record<string, any>): Promise<object | [] | string | number | boolean | null>
  • Call the server method using params as one argument construction.

    throws

    one of these errors:

    • When the method doesn't present on the server side
    • When the method call on the server triggered an exception

    Parameters

    • method: string

      The name of the remote method to call

    • Optional params: Record<string, any>

      Method arguments

    Returns Promise<object | [] | string | number | boolean | null>

    Returns a Promise with the JSON response from the client. It can be an object, an array or a primitive.

close

  • close(code?: undefined | number, reason?: undefined | string): void
  • Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason.

    Parameters

    • Optional code: undefined | number
    • Optional reason: undefined | string

    Returns void

dispatchEvent

  • dispatchEvent(event: Event): boolean
  • Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

    Parameters

    • event: Event

    Returns boolean

Private prepareContext

register

  • Register the method on the client side, handler must return an object or a Promise<object> which will be held on the server side

    example
    client.register('ping', (params) => {
      console.log('client ping', params);
      return Promise.resolve({ client: 'pong' });
    });

    You can throw an exception in the handler and on the caller side the server will catch the rejection of the calling promise.

    example
    client.register('exception', () => {
      throw new Error('client exception');
    });
    
    // server
    server.call('<YOUR_TOKEN>', 'exception', {})
      .catch((e) => console.log(e.message)); // prints "client exception"

    If the function does not return a value, a value of null will be obtained on the client side. Because there is no undefined value in the JSON representation.

    Parameters

    Returns void

removeEventListener

  • removeEventListener<K>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): void
  • removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void
  • Type parameters

    • K: keyof WebSocketEventMap

    Parameters

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

          Returns any

    • Optional options: boolean | EventListenerOptions

    Returns void

  • Parameters

    • type: string
    • listener: EventListenerOrEventListenerObject
    • Optional options: boolean | EventListenerOptions

    Returns void

send

  • send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void
  • Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView.

    Parameters

    • data: string | ArrayBufferLike | Blob | ArrayBufferView

    Returns void

Generated using TypeDoc