This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Managing a Connection
|
89
Whenever a Flash movie connects to an application instance, the onConnect( )
method of the instance’s
application object is called. The first parameter passed to
onConnect( ) is always a Client object that represents the client-side Flash movie. The
remaining parameters are the parameters passed into the connect( ) method in the
movie.
Therefore, when a client attempts to connect to the testLobby FlashCom applica-
tion, the main.asc file shown in Example 3-3 checks the username and password
passed in. A full-fledged version could validate the username and password against a
database of valid users.
Using a Connection
A NetConnection object represents a network connection within a Flash movie. Any
object that communicates over a network connection must have access to a NetCon-
nection object. For example, the NetStream class requires a NetConnection object be
passed into its constructor function:
myStream_ns = new NetStream(myNetConnection);
Working with the NetStream class is covered in Chapter 5.
Similarly, a remote shared object’s connect( ) method must be passed a NetConnec-
tion object:
myRemote_so = SharedObject.getRemote("SOName", myNetConnection.uri, true);
myRemote_so.onStatus = onStatusFunction;
if (myRemote_so.connect(myNetConnection)) {
trace("Connection ok so far...");
}
Macromedia designed both the NetStream and SharedObject classes so that, with
some exceptions, they could be used as soon as a NetConnection object was passed
Example 3-3. Server-side script (main.asc) to accept or reject connections based on username and
password
/* The application.onConnect( ) method handles each client
* connection request. In this case, users who log in
* with userName "Guest" and password "Guest" are
* allowed to connect. Other connection requests are rejected.
*/
application.onConnect = function (client, userName, password) {
if (userName == "Guest" && password == "Guest") {
client.writeAccess = ""; // Don't give write access.
application.acceptConnection(client);
}
else {
application.rejectConnection(client,
{message: "This application has refused your connection."});
}
};

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.