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 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 created 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 connection is closed, you should perform any required cleanup, such as closing objects and components that depend on the connection. Second, you must perform any preparatory workat minimum you'll usually put in place a different onStatus( ) handlerbefore 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 button 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.
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 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.
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.