Options
All
  • Public
  • Public/Protected
  • All
Menu

Class RoomManager

The RoomManager class provides access to Room objects, and utilities for creating, destroying, joining, and observing rooms on the server.

To access the client-side representation of a room, use RoomManager's getRoom() method, which returns a Room object.

The set of rooms accessible via getRoom() is limited by the current client's knowledge of the rooms on the server. The current client can gain knowledge of a room in the following ways:

  • By watching for rooms (see RoomManager's watchForRooms() method)
  • By joining rooms (see RoomManager's joinRoom() method)
  • By observing rooms (see RoomManager's observeRoom() method)

To create a new server-side room, use the RoomManager class's createRoom() method. When createRoom() executes, the RoomManager first creates and returns a Room object, and then sends a message to the server requesting that the room be created server-side. The client application can immediately begin working with the Room object, for example, by invoking join() on it.

The join request is sent to the server after the create-room request. If the create-room attempt is rejected, and the server does not actually create the room, the client is informed of the failure, and the RoomManager automatically removes its reference to the corresponding Room object. The join attempt also fails, triggering the RoomEvent.JOIN_RESULT event.

Hierarchy

  • EventDispatcher
    • RoomManager

Index

Constructors

constructor

Properties

Readonly target

target: EventDispatcher

Methods

addEventListener

  • addEventListener(type: string, listener: Function, thisArg: any, priority?: number): boolean
  • Parameters

    • type: string
    • listener: Function
    • thisArg: any
    • Default value priority: number = 0

    Returns boolean

clientIsKnown

  • clientIsKnown(clientID: string): boolean
  • Returns true if the client with the specified clientID is an occupant or observer of any of the room manager's currently known rooms.

    Parameters

    • clientID: string

    Returns boolean

createRoom

  • The createRoom() method creates a new Room object on the client and attempts to create the matching room on the server. If roomID is specified, the new room is given the supplied identifier; otherwise, the server generates the room's identifier automatically. The result of the room-creation attempt is returned via a RoomManagerEvent.CREATE_ROOM_RESULT event.

    Parameters

    • roomID: string

      The fully qualified id of the new room, as a string. For example, "examples.chat". If roomID is not specified, the room id will be generated automatically by the server and returned via RoomManagerEvent.CREATE_ROOM_RESULT.

    • Default value roomSettings: RoomSettings = new RoomModules()

      A RoomSettings object containing the initial settings for the new room. For details, see the RoomSettings class.

    • attributes: {}[]

      An array of JavaScript objects that describes the initial room attributes for the room in the following format* [ attribute: { name:"attrName1", value:"attrValue1", shared:true, persistent:false, immutable:false }, attribute: { name:"attrName2", value:"attrValue2", shared:true, persistent:false, immutable:false } ]

    • Default value modules: RoomModules = new RoomModules()

      A RoomModules object specifying the server-side modules for the room. For details, see the RoomModules class.

    Returns Room | null

    If the roomID parameter is not null, returns a Room object representing the room the client wishes to create. If the roomID parameter is null, returns null (in which case, the current client is expected to retrieve access to the room after the dynamically generated roomID has been returned via RoomManagerEvent.CREATE_ROOM_RESULT).

    Note that when two different clients sequentially invoke createRoom(), the first create-room request will succeed, triggering a RoomManagerEvent.CREATE_ROOM_RESULT event on the first client, with a status of Status.SUCCESS. The second create-room request will trigger a RoomManagerEvent.CREATE_ROOM_RESULT event on the second client, with a status of Status.ROOM_EXISTS. The architecture of "all clients attempt to create the room, but only the first succeeds" is common in Orbiter because it produces less traffic than the architecture of "check if the room exists first before asking to create it."

dispatchEvent

  • dispatchEvent(event: Event): void
  • Parameters

    • event: Event

    Returns void

disposeCachedRooms

  • disposeCachedRooms(): void
  • Forcibly removes all rooms from this RoomManager's room cache. Rooms are potentially added to the room cache in the following situations:

    In each of the preceding cases, if the RoomManager does not already have a reference to a Room object with the specified roomID, the RoomManager optimistically creates a Room object in anticipation of the room's likely future existence.

    If the RoomManager does not already have a reference to a room with the room id "examples.chat", it makes a Room object for that id, adds it to the room cache, then sends a "join room" request to Union Server. If the room exists on the server, and the server reports that the client successfully joined the room, then the RoomManager adds the Room object to its list of "occupied rooms". If the client leaves the room, the RoomManager removes the Room object from its list of occupied rooms, but does not remove the room from the room cache. Instead, the Room object remains in the cache in anticipation of the possibility that the client might join the room again in the future. If the current client ever learns that the room has been removed from the server, the RoomManager removes the room from its room cache. Otherwise, the Room object is never removed from the cache, and application code is responsible for manually purging the cache by invoking disposeCachedRooms(). When a Room object is removed from the room cache, if the RoomManager has no other references to that object, then the Room object's data is disposed before the Room object is removed from the cache, and the Room object can no longer be used to communicate with Union Server.

    Returns void

getListeners

  • getListeners(type: string): EventListener[]
  • Parameters

    • type: string

    Returns EventListener[]

getNumRooms

  • getNumRooms(qualifier?: undefined | string): number
  • Returns the number of rooms with the specified qualifier currently known to the RoomManager. Rooms that exist on the server but are unknown to the client-side room manager are not reflected in the value returned by getNumRooms(). To synchronize the RoomManager with the server's room list, use watchForRooms (which automatically keeps the RoomManager synchronized with the server's room list).

    Parameters

    • Optional qualifier: undefined | string

      Specifies the qualifier for which a room count should be returned. For example, getNumRooms("example") returns the number of rooms with roomIDs qualified by "example". If qualifier is omitted, getNumRooms() returns the total number of rooms known to the RoomManager.

    Returns number

    An integer specifying the number of rooms with the specified qualifier known.

getRoom

  • getRoom(roomID: string): Room | null
  • Returns a reference to the Room instance specified by roomID. If no such Room instance exists, returns null.

    Parameters

    • roomID: string

      The fully qualified identifier of the Room object to retrieve.

    Returns Room | null

getRoomClassRegistry

  • Returns the RoomManager's RoomClassRegistry object, which catalogs all client-side room classes.

    Returns RoomClassRegistry

getRoomIDs

  • getRoomIDs(): string[]
  • Returns an array of roomIDs for the Room objects known to this RoomManager. The array is a one-time copy of the roomIDs in the RoomManager's known room list, and is not updated after the call to getRoomIDs().

    To retrieve an array of Room instances instead of room IDs, use RoomManager.getRooms method.

    Returns string[]

    An array of string room ids.

getRooms

  • getRooms(): Room[]
  • Returns an array of the Room objects known to this RoomManager. The list includes rooms the RoomManager has confirmed as existing only, and does not include any rooms the RoomManager has speculatively created in anticipation of incomplete operations such as a joinRoom() call. The array is a one-time copy of the RoomManager's known room list, and is not updated after the call to getRooms().

    Returns Room[]

getRoomsWithQualifier

  • getRoomsWithQualifier(qualifier?: undefined | string): Room[]
  • Returns an array of Room objects with the specified room qualifier. The array is a one-time snapshot, and is not updated. If no qualifier is specified, returns all rooms (identical to getRooms).

    Parameters

    • Optional qualifier: undefined | string

    Returns Room[]

    An array containing Room instances (or instances of subclasses of Room.

hasCachedRoom

  • hasCachedRoom(roomID: string): boolean
  • Returns true if the RoomManager has a Room object for the specified roomID in its list of cached rooms; otherwise, returns false. See watchForRooms.

    Parameters

    • roomID: string

      A fully qualified room id, such as "examples.chat".

    Returns boolean

hasListener

  • hasListener(type: string, listener: Function, thisArg: any): boolean
  • Parameters

    • type: string
    • listener: Function
    • thisArg: any

    Returns boolean

hasObservedRoom

  • hasObservedRoom(roomID: string): boolean
  • Returns true if the current client is known to be observing the specified room; otherwise, returns false. See observeRoom.

    Parameters

    • roomID: string

      A fully qualified room id, such as "examples.chat".

    Returns boolean

hasOccupiedRoom

  • hasOccupiedRoom(roomID: string): boolean
  • Returns true if the current client is known to be in the specified room; otherwise, returns false. See joinRoom.

    Parameters

    • roomID: string

      A fully qualified room id, such as "examples.chat".

    Returns boolean

hasWatchedRoom

  • hasWatchedRoom(roomID: string): boolean
  • Returns true if the RoomManager has a Room object for the specified roomID in its watched rooms list; otherwise, returns false. See watchForRooms.

    Parameters

    • roomID: string

      A fully qualified room id, such as "examples.chat".

    Returns boolean

isWatchingQualifier

  • isWatchingQualifier(qualifier: string): boolean
  • Indicates whether the current client is currently watching the specified qualifier.

    Parameters

    • qualifier: string

    Returns boolean

joinRoom

  • joinRoom(roomID: string, password: string, updateLevels: UpdateLevels): Room | null
  • Asks the server to place the current client in the server-side room. If the room is not already represented by a client-side room object, one is created and returned. When the result of the attempt is received, a RoomEvent.JOIN_RESULT event is dispatched via both the RoomManager and the joined Room. If the request attempt succeeds, a RoomEvent.JOIN event is also dispatched via the Room. The Room object will subsequently be kept updated in accordance with the current client's specified update levels (see the UpdateLevels class).

    RoomManager's joinRoom() method is functionally identical to Room.join, method, except that it can be used without the prior existence of a Room object.

    Parameters

    • roomID: string

      The fully qualified room ID of the room to join.

    • password: string

      The optional string password used to enter the room.

    • updateLevels: UpdateLevels

      Specifies the client's update levels for the room, which dictate the amount of information the client receives about the room while it is in or observing the room. See the UpdateLevels class for details.

    Returns Room | null

    The room being joined.

observeRoom

  • observeRoom(roomID: string, password?: undefined | string, updateLevels?: UpdateLevels): Room | null
  • The observeRoom() method provides a means for a client to spectate a room's state and activity without actually being in that room. For example, a chat moderator client might observe a chat room to spectate the chat without actually participating in it.

    When observeRoom() is invoked, the client sends a request to begin observing the specified room. If the request is successful, the corresponding client-side Room object will be synchronized with the server, and will subsequently be kept updated in accordance with its specified update levels (see the UpdateLevels class).

    When an observation request for a room completes, the RoomEvent.OBSERVE_RESULT event is dispatched via the RoomManager and the observed Room. If the request succeeds, RoomEvent.OBSERVE is also dispatched via the Room.

    A client can observe a room, and then join and leave it arbitrarily without affecting observation status.

    RoomManager's observeRoom() method is functionally identical to Room.observe, method, except that it can be used without the prior existence of a Room object.

    Parameters

    • roomID: string

      The fully qualified room ID of the room to observe.

    • Optional password: undefined | string

      The optional string password used to observe the room.

    • Optional updateLevels: UpdateLevels

      Specifies the client's update levels for the room, which dictate the amount of information the client receives about the room while it is in or observing the room. See the UpdateLevels class for details.

    Returns Room | null

    A reference to the room being observed, or null if the observation request could not be sent.

removeEventListener

  • removeEventListener(type: string, listener: Function, thisArg: any): boolean
  • Parameters

    • type: string
    • listener: Function
    • thisArg: any

    Returns boolean

removeRoom

  • removeRoom(roomID: string, password?: undefined | string): void
  • Asks the server to remove the specified room. The result of the attempt triggers the RoomManagerEvent.REMOVE_ROOM_RESULT event on the current client. If the removal succeeds, and there is a client-side Room object corresponding to the room in question, that Room object is automatically removed.

    Clients in the room at the time of removal are automatically forced to leave the room, triggering a RoomEvent.LEAVE event.

    Parameters

    • roomID: string

      The fully qualified identifier of the room to remove. For example, "examples.chat".

    • Optional password: undefined | string

      The password required to remove the room.

    Returns void

roomIsKnown

  • roomIsKnown(roomID: string): boolean
  • Returns true if the specified room is known to the RoomManager. A room is known when the current client successfully is in or observing it, or when it is in the RoomManager's watched room list (see watchForRooms).

    Parameters

    • roomID: string

    Returns boolean

sendMessage

  • sendMessage(messageName: string, rooms: string[], includeSelf: boolean, filters?: Filter, ...rest: string[]): void
  • Sends a message to clients in the room(s) specified by rooms. To send a message to clients in a single room only, use Room's sendMessage() method.

    Clients that prefer not to receive messages for a room can opt out of messages via the Room class's setUpdateLevels() method.

    To receive the message, recipient clients must register a message listener via MessageManager's addMessageListener() method.

    The message listener must define mandatory parameters; for details, see the MessageManager class's addMessageListener() method.

    To send a message to all rooms whose IDs are directly qualified by a given qualifier (non-recursive), specify that qualifier, followed by an asterisk, for sendMessage()'s rooms parameter.

    To send a message to all clients on the server, use the Server class's sendMessage() method.

    Parameters

    • messageName: string

      The name of the message to send.

    • rooms: string[]

      The room(s) to which to send the message. Each entry in the rooms array must be either a fully qualified room ID, or a room ID qualifier.

    • includeSelf: boolean

      Indicates whether to send the message to the current client (i.e., the client that invoked sendMessage()). Defaults to false (don't echo to the sender).

    • Optional filters: Filter

      Specifies one or more filters for the message. A filter specifies a requirement that each client must meet in order to receive the message. For example, a filter might indicate that only those clients with the attribute "team" set to "red" should receive the message. For complete details, see the IFilter interface. If filters is null, all interested clients in rooms receive the message.

    • Rest ...rest: string[]

    Returns void

setWatchedRooms

  • setWatchedRooms(qualifier: string, newRoomIDs: string[]): void
  • Sets the rooms for a given room qualifier.

    Parameters

    • qualifier: string
    • newRoomIDs: string[]

    Returns void

stopWatchingForRooms

  • stopWatchingForRooms(roomQualifier?: undefined | string): void
  • Asks the server to stop watching for rooms. In response, the server no longer sends notifications when a room with the specified roomQualifier is added or removed.

    The result of a stopWatchingForRooms() request is returned via a RoomManagerEvent.STOP_WATCHING_FOR_ROOMS_RESULT event.

    Parameters

    • Optional roomQualifier: undefined | string

      The roomID qualifier to stop watching (e.g., "examples.chat.sports"). To stop watching for rooms with the unnamed qualifier, set roomQualifier to the empty string (""). To stop watching for all rooms on the server, omit roomQualifier.

    Returns void

watchForRooms

  • watchForRooms(roomQualifier?: undefined | string): void
  • Asks the server to send a notification any time a room with the specified qualifier is created or removed. The notifications trigger either a RoomManagerEvent.ROOM_ADDED event or a RoomManagerEvent.ROOM_REMOVED event. Clients typically use watchForRooms() to create an application lobby, where a dynamic list of rooms is presented to the user for selection.

    The result of a watchForRooms() request is returned via RoomManagerEvent.WATCH_FOR_ROOMS_RESULT.

    Parameters

    • Optional roomQualifier: undefined | string

      The roomID qualifier to watch (e.g., "examples.chat.sports"). To watch for rooms with the unnamed qualifier, set roomQualifier to the empty string (""). To watch for all rooms on the server, omit.

    Returns void