Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Server

Websocket server class which accepts the connection from the clients, checks their tokens. It can register any method to call it by the client. And also can call any method on the client by its token and receive response data.

The main parts of this class are:

example
import Server from '@noveo/dual-rpc-ws';

const server = new Server({
  port: 8081,
  handshake: async (token) => {
    console.log('connected', token);
    this.call(token, 'hi', {message: 'hello from server'});
    return Promise.resolve(true);
  },
});

server.register('hi', (token, params) => {
  console.log('server hi', params);
  return Promise.resolve(`${token}, hello`);
});

Hierarchy

Index

Constructors

constructor

Events

rpcClose

  • rpcClose(token: Id): void
  • Fires when the connection with the client closed

    example
    rpc.on('rpcClose', (token) => console.log(`Client disconnected ${token}`));

    Parameters

    Returns void

Properties

clients

clients: Set<WebSocket>

Private devices

devices: Map<Id, WebSocket>

Private methods

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

options

options: ServerOptions

path

path: string

Private requests

requests: Map<Id, Request>

Private responseInProgress

responseInProgress: number = 0

Accessors

stats

Methods

addListener

  • addListener(event: "connection", cb: (client: WebSocket) => void): this
  • addListener(event: "error", cb: (err: Error) => void): this
  • addListener(event: "headers", cb: (headers: string[], request: IncomingMessage) => void): this
  • addListener(event: "close" | "listening", cb: () => void): this
  • addListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    Returns this

  • Parameters

    • event: "error"
    • cb: (err: Error) => void
        • (err: Error): void
        • Parameters

          • err: Error

          Returns void

    Returns this

  • Parameters

    • event: "headers"
    • cb: (headers: string[], request: IncomingMessage) => void
        • (headers: string[], request: IncomingMessage): void
        • Parameters

          • headers: string[]
          • request: IncomingMessage

          Returns void

    Returns this

  • Parameters

    • event: "close" | "listening"
    • cb: () => void
        • (): void
        • Returns void

    Returns this

  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

address

call

  • call(token: Id, method: Name, params?: Record<string, any>): Promise<JSONValue>
  • Call the client's method by the token and method name using params as one argument construction.

    throws

    one of these errors:

    • When the client with token doesn't connected
    • When the method doesn't present on the client side
    • When the method call on the client side triggered an exception

    Parameters

    • token: Id

      Unique token of the client

    • method: Name

      The name of the remote method to call

    • Optional params: Record<string, any>

      Method arguments

    Returns Promise<JSONValue>

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

close

  • close(cb?: undefined | ((err?: Error) => void)): void
  • Parameters

    • Optional cb: undefined | ((err?: Error) => void)

    Returns void

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

eventNames

  • eventNames(): Array<string | symbol>
  • Returns Array<string | symbol>

getMaxListeners

  • getMaxListeners(): number

handleUpgrade

  • handleUpgrade(request: IncomingMessage, socket: Socket, upgradeHead: Buffer, callback: (client: WebSocket) => void): void
  • Parameters

    • request: IncomingMessage
    • socket: Socket
    • upgradeHead: Buffer
    • callback: (client: WebSocket) => void

    Returns void

Private handshake

  • handshake(_: string): Promise<true>

listenerCount

  • listenerCount(type: string | symbol): number
  • Parameters

    • type: string | symbol

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

off

  • off(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

on

  • on(event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this
  • on(event: "error", cb: (this: Server, error: Error) => void): this
  • on(event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this
  • on(event: "close" | "listening", cb: (this: Server) => void): this
  • on(event: string | symbol, listener: (this: Server, ...args: any[]) => void): this
  • Parameters

    • event: "connection"
    • cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void

    Returns this

  • Parameters

    • event: "error"
    • cb: (this: Server, error: Error) => void
        • (this: Server, error: Error): void
        • Parameters

          Returns void

    Returns this

  • Parameters

    • event: "headers"
    • cb: (this: Server, headers: string[], request: IncomingMessage) => void
        • (this: Server, headers: string[], request: IncomingMessage): void
        • Parameters

          • this: Server
          • headers: string[]
          • request: IncomingMessage

          Returns void

    Returns this

  • Parameters

    • event: "close" | "listening"
    • cb: (this: Server) => void

    Returns this

  • Parameters

    • event: string | symbol
    • listener: (this: Server, ...args: any[]) => void
        • (this: Server, ...args: any[]): void
        • Parameters

          • this: Server
          • Rest ...args: any[]

          Returns void

    Returns this

once

  • once(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

Private prepareContext

prependListener

  • prependListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

  • rawListeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

register

  • Register the method on the server side, handler accepts token of the client it was called and params as an object and must return an object or a Promise<object> which will be held on the client side.

    First argument of the handler is the context object, which contains by the default token and unique message id of the rpc call

    example
    rpc.register('ping', (ctx, params) => {
      console.log(ctx.id, 'server ping from', ctx.token, params);
      return Promise.resolve({ server: 'pong' });
    });

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

    example
    rpc.register('exception', () => {
      throw new Error('server exception');
    });
    
    // client
    ws.call('exception', {})
      .catch((e) => console.log(e.message)); // prints "server 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

removeAllListeners

  • removeAllListeners(event?: string | symbol): this
  • Parameters

    • Optional event: string | symbol

    Returns this

removeListener

  • removeListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

setMaxListeners

  • setMaxListeners(n: number): this

shouldHandle

  • shouldHandle(request: IncomingMessage): boolean
  • Parameters

    • request: IncomingMessage

    Returns boolean

Generated using TypeDoc