Reconnecting with the ActionScript Classic API
The ActionScript® Classic API supports reconnection. If you have reconnection enabled and you lose your connection to a server, you can reestablish it, using the same client ID and with the client subscriptions preserved.
Liveness monitor
The ActionScript Classic API implements a liveness monitor that listens for pings from the server and raises an event if the connection to the server is lost.
client.enableLivenessMonitor(true); client.connect(connectionDetails);
The Diffusion server sends out pings at regular intervals. The length of this interval is configured at the server by using the system-ping-frequency element in the Connectors.xml configuration file.
The liveness monitor in the ActionScript client library listens for the pings from the server and uses them to estimate the ping interval. The liveness monitor takes an average of the time between the pings it receives to estimate the ping interval. It revises this estimation each time it receives a ping, until it has received ten pings. After ten pings the liveness monitor has obtained the estimated ping interval that it uses for the rest of the client session.
If the liveness monitor does not receive a ping within a time interval equal to twice the length of the estimated ping interval, it considers the connection lost and raises a DiffusionConnectionEvent whose hasLostConnection() method returns true.
You can implement an event listener in your client that listens for this event and reacts to it by using the reconnect() method to reestablish the connection.
The liveness monitor relies on server pings being received at regular intervals. If the server pings the client in addition to the regular pings, these additional pings can cause the liveness monitor to make an incorrect estimate of the ping interval. Because this incorrect estimate is shorter than the correct ping interval, this can cause the liveness monitor to incorrectly consider a connection lost.
To avoid this problem, if you are using the liveness monitor, ensure that you do not ping the client from a publisher or from the Introspector.
Reconnection example
To reconnect, you must use the reconnect method when you lose a connection. You cannot reconnect an aborted client.
The following code shows how to setup an event listener for connection events, if the connection has been lost how to reconnect and how to tell if you have successfully reconnected the client.
var client:DiffusionClient = createClient(); function onConnectionEvent(event:DiffusionConnectionEvent) { if (event.hasLostConnection()) { client.reconnect(); } else if (event.isConnected()) { if (event.isReconnected()) { // Successful reconnection } } } function createClient():DiffusionClient { var client:DiffusionClient = new DiffusionClient(); client.addEventListener(DiffusionConnectionEvent.CONNECTION, onConnectionEvent); return client; }