This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
106
|
Chapter 3: Managing Connections
Placing the main.asc file from Example 3-13 into an applications subdirectory named
netConnectChat allows this server-side main.asc script to control the netConnectChat
application. If you were developing the main.asc script, you could always test it with
a test client but let’s push on to create the client-side part of the netConnectChat
application in the next section.
Building the Client
Let’s build a Flash movie to connect to our brand new netConnectChat application.
For now, we’ll stick with ActionScript 1.0 and the v1 UI components on which Mac-
romedia’s communication components rely. In Chapter 13, when we develop our
own custom communication components, we’ll switch to ActionScript 2.0 and the
v2 UI components.
Using Macromedia’s communication components without SimpleConnect is a two-
step process: establish a network connection to the application instance and then
pass the NetConnection object to each component’s connect( ) method. The netCon-
nectChat application uses the PeopleList, Chat, and UserColor components to create
a simple text chat interface. The interface can be created with everything on a single
frame, as is popular when using the SimpleConnect component, or the different
states of the movie can be spread over the timeline. The book’s web site includes an
example of a single-frame movie. In this example, we’ll build the client-side movie
for this application using the timeline. Figure 3-4 shows the timeline and Stage when
the movie is on the
Chat frame.
The
Chat frame includes the following movie clips (components) and text field:
chat_mc
A Chat component instance
peopleList_mc
A PeopleList component instance
userColor_mc
A UserColor component instance
userName_txt
The text field at the bottom of the Stage
application.acceptConnection(client);
};
// When a client disconnects, remove the username from the
// users hash so someone can use it again.
application.onDisconnect = function (client) {
var userName = gFrameworkFC.getClientGlobals(client).username;
delete users[userName];
};
Example 3-13. The main.asc file for the netConnectChat application (continued)
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Communication Components Without SimpleConnect
|
107
connect_btn
The Connect button at the bottom of the Stage
The
Login frame contains only the userName_txt field and connect_btn button. If we
were using the SimpleConnect component to log in the user, any other communica-
tion component would have to exist throughout the same timeline frames as Simple-
Connect. In this example, the communication components do not need to be on the
same frames as the username field or the Connect button. The components need
only a NetConnection to function, so the username field and Connect button don’t
have to be included in the
Chat frame either. However, in the Chat state, we change
the Connect button label to Disconnect; the button logs out the user when clicked.
The username text field is kept on the Stage to show the current user’s name, but it is
disabled during the chat.
The Scripts layer contains all but two lines of the script for the movie. The three
important frames are the
Init frame, which contains most of the script, and the
Login and Chat frames. When the script on the Init frame has completed executing,
the playhead moves to the
Login frame. There is only one line of code on the Login
frame of the Scripts layers:
startLoginState( );
This statement calls the startLoginState( ) function declared in the first frame of the
timeline to initialize the elements on the Stage:
function startLoginState ( ) {
connect_btn.setLabel("Connect");
userName_txt.selectable = true;
}
Figure 3-4. The timeline and Stage with the playhead on the Chat frame

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.