This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
104
|
Chapter 3: Managing Connections
All the preceding examples are available in a Zip archive file from http://flash-
communications.net. The archive includes the file com/oreilly/pfcs/FCSConnector.as.
That file must be extracted from the archive with its path intact (most unZip pro-
grams have a Maintain Folders option for this purpose) and placed in a directory in
the classpath of Flash MX 2004. See Chapter 9 of Essential ActionScript 2.0
(O’Reilly) for more information on packages and the classpath.
Communication Components Without
SimpleConnect
Chapter 2 showed how applications can be created using Macromedia’s communica-
tion components. Macromedia supplies the SimpleConnect component to manage a
network connection and connect all the other components to it. SimpleConnect
allows users to log in using any name when they connect—even the same name
someone else is using. If you need to develop an application that manages user iden-
tities differently but want to use Macromedia’s communication components, there
are two options. One is to write your own connection component. Chapters 13
through 15 describe how to build custom components. The other option is not to
use a connection component at all, as illustrated in the final example in Chapter 2.
The following example uses a little server-side scripting, a NetConnection subclass,
and the communication components to demonstrate creating a basic chat room
application with separate login and chat screens. The application enforces unique
usernames, doesn’t allow name changes while connected, and does not permit lurk-
ing. It is not designed to provide a lobby and multiple chat rooms. These and other
enhancements are added in later chapters.
Creating the Application on the Server
To use the communication components without SimpleConnect, an application’s
main.asc file must load the component framework and store a username for every cli-
ent that connects within the framework. A minimal main.asc file is shown in
Example 3-12.
button.label = "Connect";
nc.close( );
}
}
function writeln (msg) {
msgTextArea.text += msg + "\n";
msgTextArea.vPosition = msgTextArea.maxVPosition;
msgTextArea.redraw( );// Fixes scrolling bug in TextArea as of Flash 7.2.
}
Example 3-11. Importing and using the FCSConnector class in the main timeline of a movie
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Communication Components Without SimpleConnect
|
105
Example 3-12 allows anyone to connect with any username. To reject connections in
which the username is blank or already in use requires a little more work.
Example 3-13 shows a listing of another main.asc file. The script uses an object
named
users to keep track of the userName associated with each Flash movie. (An
object such as
users, in which the item name is used to access array elements, is
known as an associative array, hash table, or simply hash.) The trim( ) function is
used to preprocess each
userName before checking whether it is null, an empty string,
or already in the
users object.
Example 3-12. Minimal main.asc file when SimpleConnect is not used
load("components.asc");
application.onConnect = function (client, userName) {
gFrameworkFC.getClientGlobals(client).username = userName;
application.acceptConnection(client);
};
Example 3-13. The main.asc file for the netConnectChat application
load("components.asc");
// Trim any whitespace from before or after the userName.
// SSAS supports regular expressions, but client-side ActionScript does not.
function trim (str) {
if (typeof str != "string") return ""; // Make sure str is a string.
str = str.replace(/^\s*/, ""); // Trim leading spaces.
str = str.replace(/\s*$/, ""); // Trim trailing spaces.
str = str.replace(/\n/g, ""); // Remove new lines.
str = str.replace(/\r/g, ""); // Remove carriage returns.
str = str.replace(/\t/g, ""); // Remove tabs.
return str;
}
// Hash of client objects using the userName as a property name.
users = {};
// The onConnect( ) method rejects connection attempts
// where userName is invalid text or is already in use.
application.onConnect = function (client, userName) {
userName = trim(userName); // Remove leading and trailing whitespace.
if (userName.length == 0) { // If it is empty, reject it.
application.rejectConnection(client, {msg: "Empty username."});
return;
}
if (users[userName]) { // If it is in use already, reject it.
application.rejectConnection(client,
{msg: 'The username "' + userName + '" is already in use.'});
return;
}
// Store a reference to the client in the users hash.
users[userName] = client;
gFrameworkFC.getClientGlobals(client).username = userName;

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.