This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
86
|
Chapter 3: Managing Connections
Dealing with Problems
When a connection cannot be established or is lost, a number of possible message
sequences can be sent to an onStatus( ) handler:
A single error code
An error code followed by a closed status
A single closed status
The
isConnected property of the NetConnection object is false when any of these
events occurs. Whenever a problem occurs, you may want your script to take action,
such as informing the user. The simplest way to deal with problems is to look at the
isConnected property in the onStatus( ) handler:
if (!this.isConnected) {
if (info.code = "NetConnection.Connect.Rejected") {
// Tell the user the connection was rejected by the application.
}
else {
// Give the user more info based on info.code and/or info.level.
}
// Clean up any objects and components that need them.
// Change state by going to another frame on the main timeline.
}
Displaying a message whenever isConnected is false may lead to the user seeing two
messages—a reject message followed by a closed message. To avoid displaying two
messages, the onStatus( ) handler must be stopped from acting on the second mes-
sage. One way to do this is to remove the onStatus( ) handler completely by setting
the
onStatus property to null after the first message arrives. This can be done inside
the onStatus( ) method:
this.onStatus = null;
Removing the onStatus( ) handler so that no further message processing can take
place may seem like a drastic solution. However, the
onStatus property can be reset
later to a method that can handle messages. Alternatively, another NetConnection
object, with a working onStatus( ) handler, can be created and used to connect again.
Another approach is to create an extra property of the NetConnection object that tog-
gles the handling of certain messages. When messages are expected after a connect( )
call is made or after a “success” status message, the property is set to
true. When a
connection has been closed, the property can be set to
false. Example 3-2 shows one
way to do this.
Closing the Connection from the Client
Another potential problem is that when you let the user close the connection via Net-
Connection.close( ), the onStatus( ) handler still receives a “closed” status message.
There is often little point in showing this message to the user since she was
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Managing a Connection
|
87
responsible for closing the connection in the first place. It may be simpler to just
change the state of the application by, for example, moving the playhead of the main
timeline. Again, a property can be stored in a NetConnection object or the onStatus( )
method can be set to
null, to avoid displaying a close message.
Example 3-2 uses a property named
handleCloseEvents to indicate if error and close
messages should be processed. This is just one way to write an onStatus( ) handler
using this type of flag, and it can be altered as requirements dictate. An example that
assigns values dynamically to the
onStatus property is included in the online version
of Example 3-2 (at http://flash-communications.net). Figure 3-1 shows the timeline for
the movie used in Example 3-2. The movie can be in one of three states, each repre-
sented by a label at the beginning of a series of frames.
Init starts on the first frame
and is played only when the movie first loads; the
Login frame is shown when the
movie is in a disconnected state and the user can try to connect; the Connected frame
is shown when the movie is in the connected state.
The script in Example 3-2, located on frame 1 of the Scripts layer of the main time-
line, displays user messages in a text field. The ActionScript sends the playhead to
either the
Login or Connected frame depending on the state of the network connec-
tion.
Figure 3-1. The timeline from Example 3-2
Example 3-2. A script that handles connection-related events
// writeln( ) writes messages into a text field named trace_txt
// and is a runtime alternative to trace( ) and the Output panel.
function writeln (msg) {
trace_txt.text += msg + "\n";
trace_txt.scroll = trace_txt.maxscroll;
}
/* A simple onStatus( ) handler that moves the playhead to Login if a
* connection is closed and to Connected when a connection is established.
* User messages are written via writeln( ) into a text window.
* The this.handleCloseEvents flag is used to make sure the user never
* sees redundant messages.
*/
NetConnection.prototype.onStatus = function (info) {
// Always deal with successful connections.
if (info.code == "NetConnection.Connect.Success") {

Get Programming Flash Communication Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.