Packagenet.user1.reactor
Classpublic class ConnectionMonitor
InheritanceConnectionMonitor Inheritance Object

Since : Reactor 1.0.0

The ConnectionMonitor class provides an application heartbeat and automatic disconnection and reconnection services. By default, the ConnectionMonitor sends a heartbeat every 10 seconds, and uses it to calculate the client's ping time. If no server response is received for over 60 seconds (the default), the ConnectionMonitor automatically closes the client connection. Applications that wish to automatically reconnect after a connection failure must set the "reconnect frequency" via setAutoReconnectFrequency(). ConnectionMonitor does not automatically reconnect by default.

Note that the ConnctionMonitor class need not be instantiated directly; instead, each Reactor object creates a ConnectionMonitor instance automatically. To retrieve a given Reactor object's ConnectionMonitor instance, use the Reactor class's getConnectionMonitor() method.

The following code causes ConnectionMonitor to send a heartbeat every 5 seconds:

   var reactor:Reactor = new Reactor();
   reactor.getConnectionMonitor().setHeartbeatFrequency(5000);
   

The ConnectionMonitor class can detect, and then automatically recover from, a dropped internet connection. Flash Player does not, itself, reliably or quickly close socket connections when a network cable is unplugged or a wireless network becomes unavailable. By contrast, the ConnectionMonitor can be configured to detect connection failure within seconds. For example, the following code causes Reactor to disconnect from Union Server when no heartbeat (ping) response is received for more than seven seconds.

   var reactor:Reactor = new Reactor();
   reactor.getConnectionMonitor().setConnectionTimeout(7000);
   

See also

setHeartbeatFrequency
setConnectionTimeout
setAutoReconnectFrequency


Public Variables
 VariableDefined By
  DEFAULT_AUTORECONNECT_ATTEMPT_LIMIT : int = -1
[static] The default value for the maximum number of reconnection attempts the ConnectionMonitor will make when automatic reconnection is enabled; defaults to -1, meaning never stop attempting to reconnect if automatic reconnection is enabled.
ConnectionMonitor
  DEFAULT_AUTORECONNECT_FREQUENCY : int = -1
[static] The ConnectionMonitor's default automatic reconnection frequency setting; defaults to -1 milliseconds, meaning don't attempt to reconnect.
ConnectionMonitor
  DEFAULT_CONNECTION_TIMEOUT : int = 60000
[static] The ConnectionMonitor's default connection-timeout setting; defaults to 60000 milliseconds (one minute).
ConnectionMonitor
  DEFAULT_HEARTBEAT_FREQUENCY : int = 10000
[static] The ConnectionMonitor's default heartbeat setting; defaults to 10000 milliseconds.
ConnectionMonitor
Public Methods
 MethodDefined By
  
Constructor.
ConnectionMonitor
  
Disables the client-side heartbeat.
ConnectionMonitor
  
Prevents heartbeat-related messages from appearing in the client-side log.
ConnectionMonitor
  
Enables the client-side heartbeat, used to calculate ping time and to detect connection loss.
ConnectionMonitor
  
Allows heartbeat-related messages to appear in the client-side log.
ConnectionMonitor
  
Returns the number of times Reactor will attempt to reconnect to Union Server when automatic reconnection is enabled.
ConnectionMonitor
  
Returns the number of milliseconds between reconnection attempts made when the connection to Union Server is lost.
ConnectionMonitor
  
Returns the maximum round-trip time allowed for a heartbeat before the client considers the connection lost and forces a disconnection.
ConnectionMonitor
  
Returns the number of milliseconds between application heartbeats.
ConnectionMonitor
  
isPingShared():Boolean
Indicates whether the current client's ping is currently shared with other clients.
ConnectionMonitor
  
Sets the monitor to its default configuration.
ConnectionMonitor
  
setAutoReconnectAttemptLimit(attempts:int):void
Specifies the number of times Reactor should attempt to reconnect to Union Server when automatic reconnection is enabled.
ConnectionMonitor
  
setAutoReconnectFrequency(minMS:int, maxMS:int = -1, delayFirstAttempt:Boolean = false):void
Specifies the number of milliseconds between reconnection attempts made when the connection to Union Server is lost.
ConnectionMonitor
  
setConnectionTimeout(milliseconds:int):void
Sets the maximum round-trip time allowed for a heartbeat before the client considers the connection lost and forces a disconnection.
ConnectionMonitor
  
setHeartbeatFrequency(milliseconds:int):void
Sets the frequency of the application's "heartbeat".
ConnectionMonitor
  
sharePing(share:Boolean):void
Specifies whether the current client's ping should automatically be shared with other clients.
ConnectionMonitor
Public Constants
 ConstantDefined By
  MIN_HEARTBEAT_FREQUENCY : int = 20
[static] The ConnectionMonitor's minimum heartbeat setting: 20 milliseconds.
ConnectionMonitor
Variable Descriptions
DEFAULT_AUTORECONNECT_ATTEMPT_LIMITvariable
public static var DEFAULT_AUTORECONNECT_ATTEMPT_LIMIT:int = -1

Since : Reactor 1.1.0

The default value for the maximum number of reconnection attempts the ConnectionMonitor will make when automatic reconnection is enabled; defaults to -1, meaning never stop attempting to reconnect if automatic reconnection is enabled.

See also

DEFAULT_AUTORECONNECT_FREQUENCYvariable 
public static var DEFAULT_AUTORECONNECT_FREQUENCY:int = -1

Since : Reactor 1.0.0

The ConnectionMonitor's default automatic reconnection frequency setting; defaults to -1 milliseconds, meaning don't attempt to reconnect.

See also

DEFAULT_CONNECTION_TIMEOUTvariable 
public static var DEFAULT_CONNECTION_TIMEOUT:int = 60000

Since : Reactor 1.0.0

The ConnectionMonitor's default connection-timeout setting; defaults to 60000 milliseconds (one minute).

See also

DEFAULT_HEARTBEAT_FREQUENCYvariable 
public static var DEFAULT_HEARTBEAT_FREQUENCY:int = 10000

Since : Reactor 1.0.0

The ConnectionMonitor's default heartbeat setting; defaults to 10000 milliseconds.

See also

Constructor Description
ConnectionMonitor()Constructor
public function ConnectionMonitor(reactor:Reactor)

Constructor.

Parameters
reactor:Reactor — The Reactor instance that this ConnectionMonitor will monitor.
Method Descriptions
disableHeartbeat()method
public function disableHeartbeat():void

Since : Reactor 1.0.0

Disables the client-side heartbeat. When the heartbeat is disabled, client ping time is not calculated, and client-side connection loss (due to, for example, a disconnected network cable) may go undetected.

See also

disableHeartbeatLogging()method 
public function disableHeartbeatLogging():void

Since : Reactor 1.0.0

Prevents heartbeat-related messages from appearing in the client-side log.

enableHeartbeat()method 
public function enableHeartbeat():void

Since : Reactor 1.0.0

Enables the client-side heartbeat, used to calculate ping time and to detect connection loss. Note that ConnectionMonitor's heartbeat is enabled by default, so enableHeartbeat() is required only when resuming a heartbeat that has been disabled via disableHeartbeat().

See also

enableHeartbeatLogging()method 
public function enableHeartbeatLogging():void

Since : Reactor 1.0.0

Allows heartbeat-related messages to appear in the client-side log.

getAutoReconnectAttemptLimit()method 
public function getAutoReconnectAttemptLimit():int

Since : Reactor 1.1.0

Returns the number of times Reactor will attempt to reconnect to Union Server when automatic reconnection is enabled.

Returns
int

See also

getAutoReconnectFrequency()method 
public function getAutoReconnectFrequency():int

Since : Reactor 1.0.0

Returns the number of milliseconds between reconnection attempts made when the connection to Union Server is lost. The return of getAutoReconnectFrequency() is always an integer between the minMS and maxMS values supplied to the most recent call to setAutoReconnectFrequency(), inclusive.

Returns
int

See also

getConnectionTimeout()method 
public function getConnectionTimeout():int

Since : Reactor 1.0.0

Returns the maximum round-trip time allowed for a heartbeat before the client considers the connection lost and forces a disconnection.

Returns
int

See also

getHeartbeatFrequency()method 
public function getHeartbeatFrequency():int

Since : Reactor 1.0.0

Returns the number of milliseconds between application heartbeats.

Returns
int

See also

isPingShared()method 
public function isPingShared():Boolean

Since : Reactor 1.0.0

Indicates whether the current client's ping is currently shared with other clients.

Returns
Boolean
restoreDefaults()method 
public function restoreDefaults():void

Since : Reactor 1.0.0

Sets the monitor to its default configuration.

See also

setAutoReconnectAttemptLimit()method 
public function setAutoReconnectAttemptLimit(attempts:int):void

Since : Reactor 1.1.0

Specifies the number of times Reactor should attempt to reconnect to Union Server when automatic reconnection is enabled. Defaults to -1 (no limit), which causes Reactor to keep retrying to connect indefinitely until a connection is established. To enable automatic reconnection, use ConnectionMonitor's setAutoReconnectFrequency() method.

Parameters

attempts:int

See also


Example
The following code causes Reactor to attempt to reconnect to Union Server a maximum of five times whenever the connection fails or closes. Reconnection attempts are made every 10000 milliseconds (i.e., 10 seconds).
     reactor.getConnectionMonitor().setAutoReconnectFrequency(10000);
     reactor.getConnectionMonitor().setAutoReconnectAttemptLimit(5);
     
setAutoReconnectFrequency()method 
public function setAutoReconnectFrequency(minMS:int, maxMS:int = -1, delayFirstAttempt:Boolean = false):void

Since : Reactor 1.0.0; Version 2.1.1 replaces single "frequency" parameter with minMS. maxMS. and delayFirstAttempt parameters.

Specifies the number of milliseconds between reconnection attempts made when the connection to Union Server is lost. When minMS is a positive integer and maxMS is greater than minMS, the actual number of milliseconds between reconnection attempts is selected randomly upon disconnection, as an integer between minMS and maxMS. The randomly selected reconnection frequency is used until the next successful disconnection. For example, if minMS is 7000, maxMS is 20000, and the randomly selected delay following a disconnection is 9000, then connection attempts will occur at 9000 ms, 18,000 ms, 27,000 ms, and so on until the client successfully reconnects. If the client disconnects again, a new value between 7000 and 20000 will be selected as the reconnection frequency. Must be greater than 0. Values under 5000 ms are not recommended.

The random selection of reconnection frequency prevents heavy simultaneous connection load during application outages. Without random reconnection frequency selection, if Union Server forcibly disconnected enough clients at the same time, and those clients were configured to attempt reconnection upon disconnection, the simultaneous reconnection attempts made by the disconnected clients could stress Union Server beyond capacity. Load can be reduced by configuring clients to reconnect after a random delay within a specified range rather than at identical intervals.

If minMS is set to -1 (the default), ConnectionMonitor will not automatically attempt to reconnect to Union Server. If an automatic reconnect attempt is currently in progress when a new reconnect attempt is scheduled to start, the existing attempt will be aborted before the new attempt starts. Therefore, on a typical internet connection, where a connection to Union server can take anywhere from 50 milliseconds to 10 seconds, setting minMS to anything lower than 5000 milliseconds can cause potentially successful connection attempts to be prematurely aborted. Use caution when applying low automatic reconnection frequencies.

Parameters

minMS:int — The minimum number of milliseconds that must elapse between reconnection attempts.
 
maxMS:int (default = -1) — The maximum number of milliseconds that can elapse between reconnection attempts. When not specified, maxMS defaults to the value specified by minMS (i.e., no random value will be selected). Must be greater than or equal to maxMS.
 
delayFirstAttempt:Boolean (default = false) — When true, upon first disconnection after a successful connection, the client will wait the amount of time specified by minMS and maxMS before attempting to reconnect. When false, upon first disconnection after a successful connection, the client will attempt to reconnect immediately.

See also


Example
The following code causes Reactor to attempt an automatic reconnection every ten seconds when the connection to Union Server is lost. Upon first being disconnected after a successful connection, Reactor will immediately attempt to reconnect.
     var reactor:Reactor = new Reactor();
     reactor.getConnectionMonitor().setAutoReconnectFrequency(10000);
     
The following code causes Reactor to attempt an automatic reconnection between five and ten seconds after the connection to Union Server is lost. Because the delayFirstAttempt parameter value is true, upon first being disconnected after a successful connection, Reactor will wait for the selected random delay to pass before peforming the first reconnection attempt.
     reactor.getConnectionMonitor().setAutoReconnectFrequency(5000, 10000, true);
     
setConnectionTimeout()method 
public function setConnectionTimeout(milliseconds:int):void

Since : Reactor 1.0.0

Sets the maximum round-trip time allowed for a heartbeat before the client considers the connection lost and forces a disconnection. Defaults to 60 seconds (60000 ms).

The connection timeout is checked immediately before each heartbeat is sent. At heartbeat-send time, if the oldest heartbeat has not received a response and the connection timeout has elapsed, Reactor disconnects.

Parameters

milliseconds:int — The maximum round-trip time allowed for a single heartbeat message, in milliseconds.

See also


Example
The following code causes Reactor to disconnect from Union Server when no heartbeat (ping) response is received for more than seven seconds.
     var reactor:Reactor = new Reactor();
     reactor.getConnectionMonitor().setConnectionTimeout(7000);
     
setHeartbeatFrequency()method 
public function setHeartbeatFrequency(milliseconds:int):void

Since : Reactor 1.0.0

Sets the frequency of the application's "heartbeat". For each heartbeat, the client attempts to send a message to itself via the server. If the attempt succeeds, the client sets its current ping time to the message round-trip time.

The ConnectionMonitor's heartbeat frequency defaults to one heartbeat every ten seconds. To disable the ConnectionMonitor's heartbeat completely, invoke disableHeartbeat().

When heartbeating is enabled in an application, if the ConnectionMonitor's current connection timeout is set to a positive value, then failure to receive a heartbeat message within the specified connection timeout triggers an automatic client-side disconnect.

Parameters

milliseconds:int — The number of milliseconds between heartbeats.

See also


Example
The following code causes Reactor to send a heartbeat (ping) once a second.
     var reactor:Reactor = new Reactor();
     reactor.getConnectionMonitor().setHeartbeatFrequency(1000);
     
sharePing()method 
public function sharePing(share:Boolean):void

Since : Reactor 1.0.0

Specifies whether the current client's ping should automatically be shared with other clients. By default, a client's ping time is not shared with other clients. When this client's ping is shared, other clients can retrieve its value via Client's getPing() method.

Parameters

share:Boolean

See also

Constant Descriptions
MIN_HEARTBEAT_FREQUENCYConstant
public static const MIN_HEARTBEAT_FREQUENCY:int = 20

Since : Reactor 1.0.0

The ConnectionMonitor's minimum heartbeat setting: 20 milliseconds.

See also