Class RPCClient

Hierarchy

  • RPCBase<Socket>
    • RPCClient

Constructors

  • Creates an instance of the RPC client.

    Parameters

    • url: string

      The URL of the WebSocket server to connect to.

    Returns RPCClient

Properties

calls: Map<number, (result: any, error: any) => void> = ...
connected: boolean = false
currentID: number = 0
events: Map<string, Signal<(...args: any[]) => void>> = ...
functions: Map<string, (...args: any[]) => any> = ...
onAuthorized: Signal<() => void> = ...
onConnected: Signal<() => void> = ...
onDisconnected: Signal<(error: any) => void> = ...
onReconnected: Signal<() => void> = ...
userInfo: undefined | UserInfo
wasConnected: boolean = false

Methods

  • Authorizes the connection with the given token.

    Parameters

    • token: string

      The token to authorize the user with.

    Returns Promise<undefined | UserInfo>

    A promise that resolves with the user info if the user is authorized, or rejects if the user is not authorized.

  • Calls a method on the server.

    Type Parameters

    • T

    Parameters

    • method: string

      The method to call on the server.

    • ...params: unknown[]

      The parameters to pass to the method.

    Returns Promise<T>

    A promise that resolves with the result of the method call, or rejects if the method call fails (throws an exception, for example).

  • Type Parameters

    • T

    Parameters

    • ws: Socket
    • method: string
    • ...params: unknown[]

    Returns Promise<T>

  • Establishes a connection if not already connected.

    Returns Promise<void>

    A promise that resolves when the connection is successfully established, or rejects if the connection isn't correctly estabilished.

  • Closes the connection to the server.

    Returns void

  • Parameters

    • ws: Socket
    • method: string
    • ...params: unknown[]

    Returns void

  • Returns the user info of the currently authorized user.

    Returns undefined | UserInfo

    The user info of the currently authorized user, or undefined if not authorized.

  • Parameters

    • ws: Socket

    Returns void

  • Registers an event listener for the specified event name. The method will be called whenever the event is emitted by server.

    Parameters

    • name: string

      The name of the event to listen for.

    • method: (...args: any[]) => void

      The callback function to be executed when the event is triggered.

    Returns SignalConnection

    A SignalConnection object that can be used to manage the connection.

  • Parameters

    • ws: Socket
    • data: RPCMessage

    Returns void

  • Parameters

    • ws: Socket
    • event: RPCEvent

    Returns void

  • Parameters

    • ws: Socket
    • data: RPCCall

    Returns void

  • Parameters

    • resp: RPCResponse

    Returns void

  • Reconnects to the server.

    Returns Promise<void>

    A promise that resolves when the connection is successfully reestablished, or rejects if the connection isn't correctly reestablished

  • Registers a function with a given name. Server can use call method to call this function.

    Parameters

    • name: string

      The name of the function to register.

    • method: (...args: any[]) => any

      The function to be registered, which can take any number of arguments and return any type.

    Returns void

  • Parameters

    • ws: Socket
    • message: RPCMessage

    Returns void

  • Parameters

    • ws: Socket
    • id: number
    • error: unknown

    Returns void

  • Parameters

    • ws: Socket
    • id: number
    • result: unknown

    Returns void