Class WebSocketAcceptor<Header, Provider, Remote>

Web Socket Acceptor.

  • available only in the NodeJS.

The WebSocketAcceptor is a communicator class interacting with the remote websocket client through RPC (Remote Procedure Call), created by the WebSocketServer class whenever a remote client connects to the websocket server.

When a remote client connects to the websocket server, so that a new WebSocketAcceptor instance being created, you can determine whether to accept the client's connection or not, reading the header and path properties. If you've decided to accept the connection, call the accept method with Provider instance. Otherwise, reject it thorugh the reject method.

After accepting the connection, don't forget to closing the connection after your business has been completed to clean up the resources. Otherwise the closing must be performed by the remote client, you can wait the remote client's closing signal by the join method.

Also, when declaring this WebSocketAcceptor type, you have to define three generic arguments; Header, Provider and Remote. Those generic arguments must be same with the ones defined in the WebSocketServer class.

For reference, the first Header type repersents an initial data from the remote client after the connection. I recommend utilize it as an activation tool for security enhancement. The second generic argument Provider represents a provider from server to client, and the other Remote means a provider from the remote client to server.

Jeongho Nam - https://github.com/samchon

Type Parameters

  • Header

    Type of the header containing initial data.

  • Provider extends object | null

    Type of features provided for the remote client.

  • Remote extends object | null

    Type of features provided by remote client.

Hierarchy

Implements

  • IWebSocketCommunicator

Accessors

Methods

  • Parameters

    • Optionalcode: number
    • Optionalreason: string

    Returns Promise<void>

  • Get Driver for RFC (Remote Function Call).

    The Controller is an interface who defines provided functions from the remote system. The Driver is an object who makes to call remote functions, defined in the Controller and provided by Provider in the remote system, possible.

    In other words, calling a functions in the Driver<Controller>, it means to call a matched function in the remote system's Provider object.

    • Controller: Definition only
    • Driver: Remote Function Call

    Type Parameters

    • Controller extends object = NonNullable<Remote>

      An interface for provided features (functions & objects) from the remote system (Provider).

    • UseParametric extends boolean = false

      Whether to convert type of function parameters to be compatible with their pritimive.

    Returns Driver<Controller, UseParametric>

    A Driver for the RFC.

  • Get current Provider.

    Get an object providing features (functions & objects) for remote system. The remote system would call the features (Provider) by using its Driver<Controller>.

    Returns undefined | Provider

    Current Provider object

  • Join connection.

    Wait until the connection to be closed.

    Returns Promise<void>

  • Join connection or timeout.

    Wait until the connection to be clsoed until timeout.

    Parameters

    • ms: number

      The maximum milliseconds for joining.

    Returns Promise<boolean>

    Whether awaken by disconnection or timeout.

  • Join connection or time expiration.

    Wait until the connection to be closed until time expiration.

    Parameters

    • at: Date

      The maximum time point to join.

    Returns Promise<boolean>

    Whether awaken by disconnection or time expiration.

  • Ping to the remote client.

    Send a ping message to the remote client repeatedly.

    The ping message would be sent every internal milliseconds, until the connection be disconnectedd. The remote client will reply with a pong message, so that the connection would be alive until be explicitly disconnected.

    Parameters

    • ms: number

      Interval milliseconds

    Returns void

    Error when the connection is not accepted.

  • Reject connection.

    Reject without acceptance, any interaction. The connection would be closed immediately.

    Parameters

    • Optionalstatus: number

      Status code.

    • Optionalreason: string

      Detailed reason to reject.

    Returns Promise<void>

  • Data Reply Function.

    A function should be called when data has come from the remote system.

    When you receive a message from the remote system, then parse the message with your special protocol and covert it to be an Invoke object. After the conversion, call this method.

    Parameters

    • invoke: Invoke

      Structured data converted by your special protocol.

    Returns void

  • Set Provider

    Parameters

    • obj: undefined | Provider

      An object would be provided for remote system.

    Returns void

  • Upgrade to WebSocket protocol.

    If you've not opened websocket server from WebSocketServer, you can still compose the WebSocketAcceptor instance by yourself, by upgrading the HTTP connection to the websocket protocol.

    For reference, this upgrade() method is useful when you're planning to make a server supporting both HTTP and WebSocket protocols, and distinguishing the protocol by the path of URL.

    Type Parameters

    • Header
    • Provider extends null | object
    • Remote extends null | object

    Parameters

    Returns void