Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ConnectionManager

The ConnectionManager class manages all connections made by an application to Union Server. In most applications, the ConnectionManager class is not used directly. Instead, connections to Union Server are made via the Orbiter.connect method, and disconnections are requested via the Orbiter.disconnect method. However, ConnectionManager provides more control over the connection process than the Orbiter class offers. Specifically, the ConnectionManager provides the following services:

  • Granular access to the following events: ConnectionManagerEvent.READY, ConnectionManagerEvent.CONNECT_FAILURE, ConnectionManagerEvent.DISCONNECT, ConnectionManagerEvent.CLIENT_KILL_CONNECT, ConnectionManagerEvent.SERVER_KILL_CONNECT. Note that all of the preceding connection-failure events also trigger OrbiterEvent.CLOSE, which provides a single, central location for code that should execute whenever the application enters a disconnected state. Applications that simply wish to know when the Orbiter object is no longer ready for use need not handle the individual ConnectionManagerEvent events, and instead should listen for OrbiterEvent.CLOSE only.
  • Multiple-host connection configuration. For example, suppose an application wishes to configure connections to two separate hosts: "example.com" on port 80, and "tryunion.com" on port 80. When the connection to "example.com" is not available, the application should connect to "tryunion.com". Instead of connecting via the Orbiter.connect method, which allows multiple ports on a single host only, the application configures its connections via the ConnectionManager, as follows:
        var connectionManager = orbiter.getConnectionManager();
        connectionManager.addConnection(new XMLSocketConnection("example.com", 80));
        connectionManager.addConnection(new XMLSocketConnection("tryunion.com", 80));
        connectionManager.connect();
    Note, however, that when socket connections are configured manually via addConnection(), any desired backup HTTP connections must also be configured manually.
  • Low-level connection-data access. The ConnectionManager.getActiveConnection method gives applications direct access to the Connection object used to transmit data between Union Server and the Orbiter application. By registering with that object for the and ConnectionEvent.RECEIVE_DATA events, the application can access the raw data being sent and received over the connection.

To access the ConnectionManager, use Orbiter.getConnectionManager method.

Hierarchy

  • EventDispatcher
    • ConnectionManager

Index

Constructors

constructor

Properties

connectionState

connectionState: ConnectionState = ConnectionState.NOT_CONNECTED

Readonly target

target: EventDispatcher

Static Readonly DEFAULT_READY_TIMEOUT

DEFAULT_READY_TIMEOUT: 10000 = 10000

Methods

addConnection

  • Adds a new IConnection object to this ConnectionManager's connection list. The connection list specifies the connections that should be attempted, in the order they were added, when connecting to Union Server. To connect to Union Server, use the Orbiter.connect method or the ConnectionManager.connect method.

    Parameters

    Returns void

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

connect

  • connect(): void
  • Attempts to connect to Union Server using the currently specified list of connection objects. If any of the connections in the list succeeds, ConnectionManager dispatches the ConnectionManagerEvent.READY event, and the Orbiter object dispatches a OrbiterEvent.READY event. If, however, all of the connections in the list fail, ConnectionManager dispatches the ConnectionManagerEvent.CONNECT_FAILURE event, and the Orbiter object dispatches a OrbiterEvent.CLOSE event.

    Applications that use ConnectionManager's connect() method directly must first add at least one Connection object to the ConnectionManager.

    Connections are attempted in the order they were added to the connection list.

    Each time the ConnectionManager moves to the next connection in its list, it triggers the ConnectionManagerEvent.SELECT_CONNECTION event.

    Note, however, that the ConnectionManager class's connect() method is not used directly by most applications. Instead, to connect to the server, applications typically use the Orbiter class's more convenient connect() method, which delegates its work to the ConnectionManager class's connect() method.

    However, note that while the Orbiter class's version of connect() is less verbose than ConnectionManager's connect() method, it cannot specify multiple hosts for multiple connections.

    Returns void

disconnect

  • disconnect(): void

dispatchEvent

  • dispatchEvent(event: Event): void
  • Parameters

    • event: Event

    Returns void

dispose

  • dispose(): void
  • Permanently disables this object and releases all of its resources. Once dispose() is called, the object can never be used again. Use dispose() only when purging an object from memory.

    To simply disconnect from Union Server, use disconnect, not dispose().

    Returns void

getActiveConnection

  • If a connection is currently connected, getActiveConnection() returns that connection; otherwise, getActiveConnection() returns null.

    Returns Connection | null

getAffinity

  • getAffinity(host: string): string
  • Returns the actual server address that will be used when a connection to the specified host is requested. When the current client connects to a Union Server cluster, and the specified host is a DNS server that forwards connection requests to a server node in the cluster, getAffinity() returns the address (public name or IP) of that server node. Similarly, when the current client connects to a DNS server that redirects to a Union Server instance, getAffinity() returns the address (public name or IP) of that instance. When the current client connects to a Union Server instance directly, the address returned by getAffinity() is always identical to the supplied host.

    Parameters

    • host: string

    Returns string

getConnectAbortCount

  • getConnectAbortCount(): number
  • Returns an integer indicating the number of times this ConnectionManager object has aborted an in-progress connection attempt since the last successful connection was made.

    Returns number

getConnectAttemptCount

  • getConnectAttemptCount(): number
  • Returns an integer indicating the number of times this ConnectionManager object has attempted to connect to the server since the last successful connection was established.

    Returns number

getConnectFailedCount

  • getConnectFailedCount(): number
  • Returns an integer indicating the number of failed connection attempts this ConnectionManager object has made since the last successful connection was established. Whenever a successful connection is established, the connect-failed count returns to zero.

    Returns number

getConnectionState

getConnections

  • Returns a one-time snapshot of the ConnectionManager's connection list, as an Array. Each element of the array is an Connection object.

    Returns Connection[]

getCurrentConnection

  • Returns the connection that would be opened next if connect were called.

    Returns Connection | null

getInProgressConnection

  • If a connection is currently in progress, getInProgressConnection() returns that connection; otherwise, getInProgressConnection() returns null.

    Returns Connection | null

getListeners

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

    • type: string

    Returns EventListener[]

getReadyCount

  • getReadyCount(): number
  • Returns an integer indicating the number of times this ConnectionManager object has successfully established a valid connection to the server. The count is increased when the ConnectionEvent.READY event occurs.

    Returns number

getReadyTimeout

  • getReadyTimeout(): number
  • The maximum time each Connection object allows for its setup phase to complete when attempting to connect to Union Server.

    Returns number

hasListener

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

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

    Returns boolean

isReady

  • isReady(): boolean
  • Returns a Boolean indicating whether the ConnectionManager currently has an active connection to Union Server.

    Returns boolean

    Returns true if Orbiter is currently connected to the server; false otherwise.

removeAllConnections

  • removeAllConnections(): void
  • Removes all Connection objects from this ConnectionManager's connection list. Any connection that is currently open will be disconnected. Until new connections are added via addConnection, subsequent calls to connect or disconnect will fail.

    Returns void

removeConnection

  • removeConnection(connection?: Connection): boolean
  • Removes the specified Connection object from this ConnectionManager's connection list. If the connection is currently open, it is disconnected before removal.

    Parameters

    Returns boolean

removeEventListener

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

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

    Returns boolean

setConnectionState

  • Sets the ConnectionManager's current state.

    Parameters

    Returns void

setGlobalAffinity

  • setGlobalAffinity(enabled: boolean): void
  • Specifies whether the current client's ConnectionManager will use global server affinity or local server affinity. When enabled is true (default), server affinity is set to global, and the ConnectionManager uses the affinity value shared by all clients in the current environment. When enabled is false, server affinity is set to local, and the ConnectionManager for the current client maintains its own, individual server affinity.

    For example, imagine a client application with two Orbiter instances, both of which connect to a Union Server cluster. The cluster is accessed via a round-robin DNS server, pool.example.com, and server affinity is global. The first Orbiter instance connects to pool.example.com, and is redirected to slave1.example.com. Upon connection, the affinity address slave1.example.com is assigned for host pool.example.com. Then the second Orbiter instance connects. Because affinity is global, the second Orbiter instance finds affinity address slave1.example.com for host pool.example.com, and connects directly to slave1.example.com, bypassing pool.example.com entirely.

    Now imagine the same application with server affinity set to local. The first Orbiter instance connects to pool.example.com, and is redirected to slave1.example.com as before. Upon connection, the affinity address slave1.example.com is assigned for host pool.example.com. Then the second Orbiter instance connects. Because affinity is local, the second Orbiter instance has no affinity established for pool.example.com, and therefore connects to pool.example.com, not slave1.example.com. The pool.example.com DNS server redirects the second Orbiter instance to slave2.example.com. Upon connection, the affinity address slave2.example.com is assigned for host pool.example.com in the second Orbiter instance's affinity map. For the duration specified by the server's affinity configuration, the first Orbiter instance communicates with slave1.example.com and the second Orbiter instance communicates with slave2.example.com. Compare with the preceding global affinity example, where the first Orbiter instance and the second Orbiter instance both communicate with slave1.example.com.

    Parameters

    • enabled: boolean

    Returns void

setReadyTimeout

  • setReadyTimeout(milliseconds: number): void
  • Sets the maximum time each Connection object allows for its setup phase to complete when attempting to connect to Union Server.

    When an Connection object attempts to connect to Union Server, it starts a "handshake" process during which client setup tasks (such as issuing a client ID) are performed. If the setup tasks are not completed within an allowed limit known as the "ready timeout," then the Connection object considers the setup process a failure and automatically aborts the connection attempt. If, on the other hand, the client setup process completes within the ready-timeout limit, then the Connection object considers the setup process a success and triggers a ConnectionEvent.READY event indicating that the connection is ready for use. The ready timeout defaults to 10 seconds, but can be changed via the setReadyTimeout() method.

    Once an Connection object achieves a ready state,Orbiter continues to monitor the connection to the server by sending regular heartbeat messages. By default, one heartbeat message is sent every 10 seconds, but the heartbeat message frequency is configurable via ConnectionMonitor.setHeartbeatFrequency method. If Union Server takes longer than 60 seconds (by default) to respond to single heartbeat, Orbiter will automatically disconnect. To change the duration that Orbiter will tolerate for a heartbeat response, use ConnectionMonitor.setConnectionTimeout method.

    Parameters

    • milliseconds: number

      The time within which a connection must achieve a ready state, in milliseconds.

      The following code lengthens the default ready timeout to 20 seconds. As a result, Orbiter will automatically disconnect from Union Server when the client-setup process does not complete within 20 seconds.

          const orbiter = new Orbiter();
          orbiter.getConnectionManager().setReadyTimeout(20000);

    Returns void