This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
90
|
Chapter 3: Managing Connections
to them. In both cases, the NetConnection object should be one that has attempted to
make a connection even if the connection is not yet established. For example the pro-
cess of sending data can be started via a NetStream.send( ) or SharedObject.send( )
method call before a connection is established. The data is held in a queue until the
connection is made. Similarly, a NetStream object can start the process of publishing
or playing audio or video before a connection is made. In this case, when the connec-
tion is made, the stream will begin to publish or play.
The important exception to this is shared object data and the shared object onSync( )
handler described in Chapter 8. As an added convenience, if onStatus( ) handlers are
not created for SharedObject and NetStream objects, the onStatus( ) handler of the
NetConnection with which they are associated will be called and passed the
SharedObject and NetStream information objects. In some cases, especially when
data is sent, these conveniences are useful, but for the most part should be avoided.
It is often easier to manage an application and provide better information to the user
if objects that depend on a network connection define their own onStatus( ) event
handlers and do nothing until a connection is successfully made.
Reusing a NetConnection Object
Often a Flash movie must connect to more than one application instance. A familiar
example is when Flash must first connect to a lobby so the user can select a chat
room to visit. In this case, the lobby may be one application and the chat rooms may
be implemented by another application. The lobby connection can be closed and
then a new NetConnection object, with a different onStatus( ) method, can be cre-
ated to connect to the chat application. Instead of creating a new NetConnection
object, the Flash movie can reuse an existing one. In theory, you can disconnect from
one application and connect to another by calling the connect( ) method with a new
target URI. When this happens, the old connection is closed and a new one is
attempted. However, two other things should normally happen. First, before a con-
nection is closed, you should perform any required cleanup, such as closing objects
and components that depend on the connection. Second, you must perform any pre-
paratory work—at minimum you’ll usually put in place a different onStatus( ) han-
dler—before connecting to the next application.
To follow through with the previous example, suppose an additional application,
named testChat, is available and that once the user is in the lobby he can click a but-
ton to visit a testChat instance named
room1. In this case, the main timeline of the
movie would require separate
Login, Lobby, and ChatRoom frames as illustrated in
Figure 3-2.
The Chat button would be placed in the
Lobby frames, and a Lobby button would be
placed within the
ChatRoom frames. Example 3-4 shows the onChat( ) function that
would be called when the Chat button is clicked.
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Reusing a NetConnection Object
|
91
In Example 3-4, event handling of close and connection error messages is turned off
so that the playhead is not sent back to the
Login frame when Flash calls lobbyChat_
nc.close( ). The example assigns a new onStatus( ) handler, named ChatRoom_
onStatus( ), to the
lobbyChat_nc object as follows:
lobbyChat_nc.onStatus = ChatRoom_onStatus;
Finally, when the connection is attempted, two global variables, userName and
password, are used to retrieve the username and password to submit, because the text
fields are no longer on the Stage when the user is in the lobby. Example 3-5 shows
the code for the ChatRoom_onStatus( ) function. It is used as the new onStatus( )
handler for the
lobbyChat_mc object, as indicated in the preceding code.
Figure 3-2. The timeline with Login, Lobby, and ChatRoom states
Example 3-4. Connecting from the lobby to a chat room
function doChat (btn) {
// Don't process the next close message.
lobbyChat_nc.handleCloseEvents = false;
// Close the connection to the lobby.
lobbyChat_nc.close( );
// Set the onStatus( ) handler, defined in Example 3-5.
lobbyChat_nc.onStatus = ChatRoom_onStatus;
// Make sure events are handled by it.
lobbyChat_nc.handleCloseEvents = true;
// Try to connect to a chat room.
if (lobbyChat_nc.connect("rtmp:/testChat/room1", userName, password)) {
writeln("Please wait. Attempting chat room connection...");
}
else {
writeln("Can't attempt connection. Is the URI correct?");
}
}
Example 3-5. The chat room onStatus( ) handler
function ChatRoom_onStatus (info) {
// Always deal with successful connections.
if (info.code == "NetConnection.Connect.Success") {
this.handleCloseEvents = true;
writeln("Success, you are connected to a chat room!");

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.