This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Creating an Authenticating Component
After importing Button and EventDispatcher, we define our class as a subclass of
MovieClip (for simplicity; we could just as well have extended UIComponent). We
then define a few self-explanatory variables that we are going to need.
In the constructor, we define the
m_prefix property. As we will see later, this compo-
nent is different than other communication components, in that it is first instanti-
ated on the server side and has a well-defined name, independent of what the client-
side developer names the AuthConnect component instance on stage. This name is
always “GateKeeper”.
The rest of the constructor code sets up the Connect button and the NetConnec-
tion. When the Connect button is clicked, the ) method is called.
When the connection is accepted, the AuthConnect.getUserData( ) method is called.
If the connection is rejected due to a bad username and/or password, the error mes-
sage is displayed in the
m_feedback_txt text field. If the connection is closed, a dis-
connected event is dispatched.
The next definition is for the getter function for
nc. This allows other components to
reach within AuthConnect and control the NetConnection directly. As you have seen
in the tester application, the Disconnect button uses to disconnect
from the server.
Then, we define the setup( ) method, which saves the server name and the applica-
tion name into local variables, used by the click( ) method to connect to the server.
The last method defined is getUserData( ), which queries the server side of the Auth-
Connect component for more information about the user who just logged in. This
information is then attached to the connected event that gets dispatched, in the
userData field of the event object. Notice how we use m_prefix to route the client-to-
server call to the component’s server-side instance.
The Server Side of AuthConnect
The server-side code for the AuthConnect component is the trickiest. Consider the
following code carefully. It is presented in excerpts with commentary here, but the
entire listing is available in a single file (AuthConnect.asc) on the book’s web site.
As before, we use try/catch to ensure that the component’s class will be defined only
once. Then, we load netservices.asc, which is required for Flash Remoting on the
server side.
We define our AuthConnect class as a subclass of FCComponent and call the init( )
method provided by the framework in its constructor. The constructor also defines
activeUsers associative array, which we use to maintain a list of users who are
currently logged in to the application. We also use this array to reject users who are
already logged in:
try {var dummy = AuthConnect;} catch (e) { // #ifndef AuthConnect

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.