Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SocketClient<API>

Basic socket client that can be used in Node or in the browser.

Type parameters

Hierarchy

Index

Constructors

constructor

Properties

Protected socket

socket: Socket = null

Socket.io Socket instance for internal use.

Protected Abstract socketHandlers

socketHandlers: SocketHandlers<API, "client", HandlerCtx<API>>

Contains implementations for the events described by the API. This guarantees compatibility with any SocketServer that implements the same API.

Static io

io: SocketIOClientStatic = (typeof window !== 'undefined') && window.io ? window.io : null

Reference to the socket.io-client library. If the client is running in the browser, it is assumed that io will be available on window.

Methods

Protected attachSocketHandlers

  • attachSocketHandlers(): void

blockEvent

  • blockEvent<Event>(event: Event, timeout?: number): Promise<any>
  • Gives the ability to block and wait for an event. Usage: await this.blockEvent('some-event');

    Type parameters

    • Event: string

    Parameters

    • event: Event
    • Default value timeout: number = 5000

    Returns Promise<any>

connect

  • connect<WaitFor>(url?: string, waitFor?: WaitFor, options?: ConnectOpts): WaitFor extends string ? Promise<any> : void
  • Attempts to connect to a SocketServer.

    Type parameters

    • WaitFor: string | null

    Parameters

    • Default value url: string = ""
    • Default value waitFor: WaitFor = null
    • Optional options: ConnectOpts

    Returns WaitFor extends string ? Promise<any> : void

disconnect

  • disconnect(): void

emit

  • emit<Event>(event: Event, ...args: API["client"][Event]["args"]): void
  • Emits an event to the connected SocketServer. TypeScript ensures that the event adheres to the API description.

    Type parameters

    • Event: keyof API["client"]

    Parameters

    • event: Event
    • Rest ...args: API["client"][Event]["args"]

    Returns void

getSocketId

  • getSocketId(): string

Protected handleEvent

  • handleEvent(ctx: unknown, event: string, ...args: any[]): void
  • Processes an incoming event with the appropriate socketHandler. If the handler returns an EventResponse, the proper event will automatically be emitted.

    Parameters

    • ctx: unknown
    • event: string
    • Rest ...args: any[]

    Returns void

isConnected

  • isConnected(): boolean

Protected reply

  • reply(ctx: any, response: any): void

Generated using TypeDoc