This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
A More Realistic Example
|
131
related to clients connecting and disconnecting from the application instance
requires a movie that connects to and disconnects from the instance. For this pur-
pose, a test movie is supplied on the book’s web site as part of the helloWorld.zip
archive. Test movies are an important tool. A good but simple test movie should
allow you to:
Enter or select different application instance names to which to connect
Enter a username and password or other connection parameters
Read all the status and error messages the client receives or generates
Easily extend it to add features as necessary
The output in Figure 4-6 shows the Live Log area when a connection is made and
then dropped from a client. The IP address of 127.0.0.1 indicates that the client was
running on the same system as the FlashCom Server.
Twenty-three minutes after the client disconnected, the instance shuts down and
produces the output shown in Figure 4-7 in the App Inspector.
The output in the Live Log area of the App Inspector shows data such as startup and
shutdown times, the client IP address and username, and when the client connects
and disconnects. The App Inspector and server-side trace( ) statements are essential
for testing and debugging server-side scripts.
A More Realistic Example
Writing and testing a simple main.asc script for helloWorld is an important first step
in becoming familiar with any new environment. However, while the helloWorld
application introduces the
application, Client, and info objects, it’s not very useful.
Figure 4-5. The App Inspector after restarting helloWorld/_definst_
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
132
|
Chapter 4: Applications, Instances, and Server-Side ActionScript
Although not a full-featured implementation, Example 4-3 is a bit more realistic
main.asc script in that it shows many of the methods already discussed working in a
more typical manner. The code in Example 4-3 demonstrates:
Accepting or rejecting client connections based on a username and password
Adding properties and methods to the Client object more simply, by adding an
object property instead of individual properties and methods
Figure 4-6. The App Inspector after a client connects to and disconnects from helloWorld/_definst_
Figure 4-7. The App Inspector after the helloWorld/_definst_ instance shuts down
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
A More Realistic Example
|
133
Limiting guest clients but not other types of clients when a maximum number of
users is reached
Using the server-side setInterval( ) function to periodically update information
about connected clients
The code listed in Example 4-3 is not commented to save space, but the version on
the web site is. Take a moment to read through the code. It is described immediately
after the listing.
Example 4-3. A more realistic main.asc
users = { Brian: {password: "secretPassword1", role: "author"},
Robert: {password: "secretPassword2", role: "author"},
Justin: {password: "secretPassword3", role: "author"},
Joey: {password: "secretPassword4", role: "author"},
Peldi: {password: "secretPassword5", role: "author"},
Guest: {password: "Guest", role: "guest"}
};
access = {author: {readAccess: "/", writeAccess: "/"},
guest: {readAccess: "/public", writeAccess: ""}
};
MAXCONNECTIONS = 5;
function User (client, userName, role) {
this.client = client;
this.userName = userName;
this.role = role;
this.connectTime = new Date().getTime( );
}
User.prototype.getTimeConnected = function ( ) {
var now = new Date().getTime( );
return (now - this.connectTime)/1000; // seconds
};
User.prototype.getPingTime = function ( ) {
var client = this.client;
if (client.ping( )) {
return client.getStats( ).ping_rtt / 2;
}
else{
return "Client is not connected."
}
};
User.prototype.getConnectionInfo = function ( ) {
return " IP: " + this.client.ip +
", user name: " + this.userName +
", connection time: " + this.getTimeConnected( ) +
", ping time: " + this.getPingTime( );
};

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.