Skip to main content

Room

Index

Properties

clients

clients: Map<number, NetworkClient>

A map of clients. Key is the id.

readonlyid

id: string

Unique ID of this room

maxClients

maxClients: number = Infinity

Number of clients that may connect to this room.

Methods

broadcast

  • broadcast<T, T2>(event: T, data: T2): void
  • Sends a message to everyone in this room.


    Type parameters

    • T = string | number
    • T2 = string | number | object | Buffer

    Parameters

    • event: T

      Unique identifier for this event. See onMessage for more info about event identifiers.

    • data: T2

      Data that will be broadcasted.

    Returns void

cleanup

  • cleanup(): void
  • Called before this room is closed.


    Returns void

isFull

  • isFull(): boolean
  • Checks if this room is full. You might override this method to account for seats reserved by whatever matchmaker you’re using.


    Returns boolean

    true if room is full

onClientAuth

  • Authenticates the client. If this method throws an exception, authentication will fail with exception message.


    Parameters

    • client: NetworkClient

      Client to authenticate

    • authString: string

      The authentication string passed from client

    Returns void

onClientJoined

  • Called when a client joins the room


    Parameters

    Returns void

onClientLeft

  • Called when a client leaves the room


    Parameters

    Returns void

onMessage

  • Registers a handler for event


    Parameters

    • event: string | number

      Unique identifier for this event.

      Using a string for event ids is not recommended as it makes the network packet considerably bigger (2 bytes for length and an extra 2 bytes for every character in string).

      You may use any number from 0 to 255.

    • handler: MessageHandler

      The MessageHandler function that will be called when this event is received.

    Returns void

processEvents

  • processEvents(): void
  • Processes all queued events. This method should be called every tick, preferably before ticking game logic.


    Returns void

send

  • Sends a message to a specific client


    Type parameters

    • T = string | number
    • T2 = string | number | object | Buffer

    Parameters

    • client: NetworkClient

      NetworkClient that will receive the message

    • event: T

      Unique identifier for this event. See onMessage for more info about event identifiers.

    • data: T2

      Data that will be sent to this client.

    Returns void