Constructors

Properties

isConnected: boolean = false
ws: WebSocket = null

Methods

  • Awaits a response for a specific message type

    Type Parameters

    • T = any

    Parameters

    • messageType:
          | "message"
          | "discover"
          | "error"
          | "register"
          | "peer_disconnected"
          | "request_public_key"
          | "public_key_response"
          | "server_question"
          | "peer_response"
          | "debug_question"

      The type of message to wait for

    • OptionalfilterFn: ((message: Message) => boolean)

      Optional function to filter messages by additional criteria

        • (message): boolean
        • Parameters

          Returns boolean

    • timeout: number = 10000

      Optional timeout in milliseconds (default: 10000)

    Returns Promise<T>

    Promise that resolves with the message payload or rejects with an error

  • Connects to the signaling server and registers the peer

    Returns Promise<void>

    Promise that resolves when connected and registered

  • Discovers all connected peers

    Returns Promise<string[]>

    Promise that resolves with an array of peer IDs

  • Adds a handler for connection state changes

    Parameters

    • handler: ConnectionStateHandler

      Function to call when connection state changes

    Returns void

  • Adds a handler for errors

    Parameters

    • handler: ErrorHandler

      Function to call when an error occurs

    Returns void

  • Adds a handler for incoming messages

    Parameters

    • handler: MessageHandler

      Function to call when a message is received

    Returns void

  • Adds a handler for peer disconnection events

    Parameters

    • handler: PeerDisconnectedHandler

      Function to call when a peer disconnects

    Returns void

  • Registers a handler for server questions

    Parameters

    • handler: ((question: any, questionId: string) => void)

      Function to call when a server question is received

        • (question, questionId): void
        • Parameters

          • question: any
          • questionId: string

          Returns void

    Returns void

  • Registers the peer with the signaling server and waits for confirmation

    Returns Promise<void>

    Promise that resolves when registration is confirmed

  • Removes a connection state change handler

    Parameters

    • handler: ConnectionStateHandler

      The handler to remove

    Returns void

  • Removes a message handler

    Parameters

    • handler: MessageHandler

      The handler to remove

    Returns void

  • Removes a peer disconnected handler

    Parameters

    • handler: PeerDisconnectedHandler

      The handler to remove

    Returns void

  • Requests a peer's public key

    Parameters

    • peerId: string

      The ID of the peer whose public key to request

    Returns Promise<Uint8Array<ArrayBufferLike>>

    Promise that resolves with the peer's public key

  • Responds to a server question

    Parameters

    • questionId: string

      The ID of the question to respond to

    • response: any

      The response to send back to the server

    Returns void

  • Sends a message to a specific peer

    Parameters

    • targetId: string

      The ID of the target peer

    • message: string

      The message to send

    Returns Promise<void>

  • Sends a message to the server and waits for a specific response type

    Type Parameters

    • T = any

    Parameters

    • message: Message

      The message to send

    • expectedResponseType:
          | "message"
          | "discover"
          | "error"
          | "register"
          | "peer_disconnected"
          | "request_public_key"
          | "public_key_response"
          | "server_question"
          | "peer_response"
          | "debug_question"

      The type of response to wait for

    • options: {
          errorHandler?: ((error: any) => void);
          filterFn?: ((message: Message) => boolean);
          retryCount?: number;
          timeout?: number;
      } = {}

      Additional options for handling the response

      • OptionalerrorHandler?: ((error: any) => void)
          • (error): void
          • Parameters

            • error: any

            Returns void

      • OptionalfilterFn?: ((message: Message) => boolean)
          • (message): boolean
          • Parameters

            Returns boolean

      • OptionalretryCount?: number
      • Optionaltimeout?: number

    Returns Promise<T>

    Promise that resolves with the response payload or rejects with an error