3.3. 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
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 button to visit a
testChat
instance named
room1
. In this case, the main timeline of the movie would require separate
Login
,
Lobby
, and
ChatRoom
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.
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?");
}
}
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
lobbyChat_nc.onStatus = ChatRoom_onStatus;
Finally, when the connection is attempted, two global
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!");
gotoAndPlay("ChatRoom");
}
// Handle messages when the connection is closed.
if (!this.isConnected && this.handleCloseEvents) {
// Handle close/error messages.
if (info.code == "NetConnection.Connect.Rejected") {
writeln(info.application.message);
writeln('Did you use the username "Guest" and password "Guest" ?');
}
else {
writeln("Error: Connection Closed.");
}
this.handleCloseEvents = false;
gotoAndPlay("Login");
}
// Handle remote method call errors here if you need to.
}
When the user is in a chat room, she can return to the lobby by clicking a Lobby button. Example 3-6 shows the code for the doLobby( ) function called when the user clicks the Lobby button. It is highly analogous to the doChat( ) function in Example 3-4. Among other things, it sets Lobby_onStatus( ) as the new onStatus( ) handler for the lobbyChat_mc object. The Lobby_onStatus( ) function declaration is not shown here, but it is very similar to the ChatRoom_onStatus( ) function declaration shown in Example 3-5.
function doLobby (btn) {
// Don't process the next close message.
lobbyChat_nc.handleCloseEvents = false;
// Close the connection to the chat room.
lobbyChat_nc.close( );
// Set the
onStatus( )
handler (definition of
Lobby_onStatus( )
is not shown).
lobbyChat_nc.onStatus = Lobby_onStatus;
// Make sure events are handled by it.
lobbyChat_nc.handleCloseEvents = true;
// Try to connect to the lobby.
if (lobbyChat_nc.connect("rtmp:/testLobby/", userName, password)) {
writeln("Please wait. Attempting lobby connection...");
}
else {
writeln("Can't attempt connection. Is the URI correct?");
}
}
Now that we've seen how to reuse a connection, let's see how we can use multiple connections at once.