More Books
Flash Communication Server
Flash Communication Server
Table of Contents
Copyright
About the Authors
Brian Lesser
Giacomo
Joey Lott
Robert Reinhardt
Justin Watkins
Foreword
Preface
What Does FlashCom Offer?
What's in This Book?
How to Use This Book
Audience
ActionScript 1.0 Versus ActionScript 2.0
Server-Side ActionScript
The flash-communications.net Site
Director, Breeze, and Other Options
Flash Video Options
Licensing and Hosting Options
Conventions Used in This Book
Voice
Using Code Examples
Safari Enabled
Comments and Questions
Acknowledgments
Part I:  FlashCom Foundation
Chapter 1.  Introducing the Flash Communication Server
Section 1.1.  Clients and Servers
Section 1.2.  Creating an Application
Section 1.3.  Real-Time Messaging Protocol
Section 1.4.  The Communication Classes
Section 1.5.  Communicating with Application Servers, Databases, and Directory Servers
Section 1.6.  Firewalls and Security
Section 1.7.  Getting Started
Section 1.8.  Hello Video!
Section 1.9.  Conclusion
Chapter 2.  Communication Components
Section 2.1.  Overview of Communication Components
Section 2.2.  Summary of Communication Components
Section 2.3.  Creating an Application that Monitorsa Connection
Section 2.4.  Building a Simple Chat Room
Section 2.5.  Adding Audio and Video to the Chat Room
Section 2.6.  Forgoing the SimpleConnect Component
Section 2.7.  Conclusion
Chapter 3.  Managing Connections
Section 3.1.  Making a Connection
Section 3.2.  Managing a Connection
Section 3.3.  Reusing a NetConnection Object
Section 3.4.  Multiple Simultaneous NetConnection Objects
Section 3.5.  Testing and Debugging Network Connections
Section 3.6.  Subclassing the NetConnection Class
Section 3.7.  Communication Components Without SimpleConnect
Section 3.8.  Conclusion
Chapter 4.  Applications, Instances, and Server-Side ActionScript
Section 4.1.  Scripting Application Instances
Section 4.2.  Differences Between Flash ActionScript and Server-Side ActionScript
Section 4.3.  The Life of an Application Instance
Section 4.4.  Running a Simple Hello World Test Script
Section 4.5.  A More Realistic Example
Section 4.6.  Instance-to-Instance Communications
Section 4.7.  Script Filenames and Locations in Detail
Section 4.8.  Testing and Debugging Server-SideScript Files
Section 4.9.  Designing Communication Applications
Section 4.10.  Conclusion
Part II:  Audio, Video, and Data Streams
Chapter 5.  Managing Streams
Section 5.1.  A Simple Publisher/Subscriber Example
Section 5.2.  Stream Names
Section 5.3.  Publishing Streams in Detail
Section 5.4.  Playing Streams in Detail
Section 5.5.  The Stream Class
Section 5.6.  Publishing and Playing ActionScript Data
Section 5.7.  Creating Synchronized Presentations
Section 5.8.  The NetStream and Stream Information Objects
Section 5.9.  Stream Enhancements and Limitations
Section 5.10.  Conclusion
Chapter 6.  Microphone and Camera
Section 6.1.  Working with Microphone/Audio Input
Section 6.2.  Working with Camera Input
Section 6.3.  Building a Message-Taking Application
Section 6.4.  Building a Surveillance Application
Section 6.5.  Conclusion
Chapter 7.  Media Preparation and Delivery
Section 7.1.  Audio and Video Compression
Section 7.2.  Converting Prerecorded Materialto FLV Format
Section 7.3.  Using Flash Pro's Media Components
Section 7.4.  Enabling Multiple Bit Rate FLVsWithin an Application
Section 7.5.  Streaming MP3 Audio
Section 7.6.  Conclusion
Part III:  Remote Connectivity and Communication
Chapter 8.  Shared Objects
Section 8.1.  Objects and Shared Objects
Section 8.2.  Getting a Shared Object in Flash
Section 8.3.  Updates and Frame Rates
Section 8.4.  Scripting Shared Objects on the Server
Section 8.5.  Temporary and Persistent Shared Objects
Section 8.6.  Proxied Shared Objects
Section 8.7.  Shared Objects and Custom Classes
Section 8.8.  Avoiding Collisions
Section 8.9.  Optimizing Shared Object Performance
Section 8.10.  Broadcasting Remote Method Callswith send( )
Section 8.11.  A Simple Video and Text Chat Application
Section 8.12.  Conclusion
Chapter 9.  Remote Methods
Section 9.1.  Why Use Calls?
Section 9.2.  The send( ) and call( ) Methods
Section 9.3.  Client-to-Server Calls
Section 9.4.  Server-to-Client Calls
Section 9.5.  Server-to-Server Calls
Section 9.6.  A Simple Lobby/Rooms Application
Section 9.7.  Debugging Calls
Section 9.8.  Advanced Topics
Section 9.9.  Conclusion
Chapter 10.  Server Management API
Section 10.1.  Connecting to the Admin Service
Section 10.2.  Using the Server Management API
Section 10.3.  Server Management API Uses
Section 10.4.  Conclusion
Chapter 11.  Flash Remoting
Section 11.1.  The Remoting Gateway
Section 11.2.  Remoting Basics
Section 11.3.  Role of Remoting in FlashCom Applications
Section 11.4.  Securing Access
Section 11.5.  Conclusion
Chapter 12.  ColdFusion MX and FlashCom
Section 12.1.  Understanding ColdFusion MXand Flash Remoting
Section 12.2.  Using Flash Remoting to Log Events
Section 12.3.  Getting a List of Streams
Section 12.4.  Using ColdFusion and FTP to Mirror Streams
Section 12.5.  Conclusion
Part IV:  Design and Deployment
Chapter 13.  Building Communication Components
Section 13.1.  Source Files
Section 13.2.  People Lists
Section 13.3.  A Simple People List
Section 13.4.  Listenable Shared Objects
Section 13.5.  Status and People List
Section 13.6.  Text Chat
Section 13.7.  Shared Text
Section 13.8.  Video Conference and Video Window
Section 13.9.  PeopleGrid
Section 13.10.  Summary
Section 13.11.  Conclusion
Chapter 14.  Understanding the Macromedia Component Framework
Section 14.1.  The Component Framework
Section 14.2.  Under the Hood of the Chat Component
Section 14.3.  Creating a Simple Component from Scratch: SharedTextInput
Section 14.4.  Creating a Container Component: SharedAddressForm
Section 14.5.  Creating an Authenticating Component
Section 14.6.  Integrating Components with Your Existing Applications
Section 14.7.  Understanding the Framework
Section 14.8.  Conclusion
Chapter 15.  Application Design Patterns and Best Practices
Section 15.1.  Shared Object Management
Section 15.2.  Moving Code to the Server
Section 15.3.  Building Façades on the Server
Section 15.4.  Server-Side Client Queues
Section 15.5.  A Framework for Recording and Playing Back Componentized Applications
Section 15.6.  Components and Component Frameworks
Section 15.7.  Conclusion
Chapter 16.  Building Scalable Applications
Section 16.1.  Coordinating Instances
Section 16.2.  Scalability and Load Balancing
Section 16.3.  Conclusion
Chapter 17.  Network Performance, Latency,and Concurrency
Section 17.1.  Latency
Section 17.2.  Bandwidth
Section 17.3.  Concurrency
Section 17.4.  Conclusion
Chapter 18.  Securing Applications
Section 18.1.  The Three A's: Authentication, Authorization, and Accounting
Section 18.2.  Authentication
Section 18.3.  Authorization
Section 18.4.  Accounting
Section 18.5.  Suggestions and References
Section 18.6.  Conclusion
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
R
S
T
U
V
W

2.1. Overview of Communication Components

There are 16 components in the communication components set. Like all Macromedia components, each contains several built-in ActionScript methods and properties, and you can change the look-and-feel (or skin) of each component to suit your particular tastes.

Some developers report difficulty creating custom skins (it may involve writing extra drawing code, changing hard-to-find assets, or dealing with components that implement the skinning API only partially). Search the FlashCoders archive or Wiki at http://chattyfig.figleaf.com for some of the discussion.

Chafic Kazoun wrote the following guide to skinning the Flash MX 2004 Components:

http://www.macromedia.com/devnet/mx/flash/articles/skinning_2004_print.html

You can use the communication components to quickly set up a proof-of-concept FlashCom application that connects one or more users to an application instance and enables live real-time interactivity between the participants. Many of the components can publish audio/video streams to a FlashCom application, while others focus on text or data sharing.

After you have installed the communication components, you can view the components in Flash:

  1. If you just installed the components and Flash was running, quit and restart Flash.

  2. Open the Components panel by choosing Window Development Panels Components.

  3. In the Components panel, expand the Communication Components node as shown in Figure 2-1.

    Figure 2-1. The communication components in the Components panel


You will likely find that most of these components have limited appeal for final production-quality Flash movies that you'll make available on a public web site.

By themselves (and unaltered), the communication components operate in an unrestricted manner on your FlashCom Server; anyone can connect to the Flash movie and start using the FlashCom application to which the movie is tied. For that reason, we strongly suggest that you restrict your development of this chapter's examples to a test server located within a private network.


The components are summarized briefly in Table 2-1. After describing each of the components, we'll show you how to create some simple FlashCom applications using them.

Table 2-1. Communication components

Component name

Description

Notes

AudioConference

Enables two or more users to communicate with each other

Nests CheckBox, ListBox, PushButton, and ScrollBar

AVPresence

Allows a user to send an audio and/or video stream to other users

Nests no other components; see VideoConference and SetBandwidth

Chat

Creates a text-based messaging interface for a Flash movie

Nests PushButton and ScrollBar; see UserColor

ConnectionLight

Displays a colored circle indicating the connection status

Nests no other components; see SimpleConnect

Cursor

Displays the positions and login names of users' cursors

Nests no other components; see UserColor

PeopleList

Displays the names of all users connected to a FlashCom application

Nests ListBox and ScrollBar

PresentationSWF

Displays a synchronized slideshow under speaker control to multiple users

Nests PushButton

PresentationText

Displays synchronized text slides under speaker control to multiple users

Nests ListBox, PushButton, and ScrollBar

RoomList

Enables a developer to create a gateway to other applications running on your FlashCom Server

Nests ListBox, PushButton, and ScrollBar

SetBandwidth

Allows a user to control how much bandwidth can be used by Camera and Microphone objects

Nests ComboBox, PushButton, and ScrollBar

SimpleConnect

Establishes the primary connection to a FlashCom application from a Flash movie client

Nests PushButton

UserColor

Allows each user to choose a color to represent her identity within the Chat and Cursor components

Nests ComboBox and ScrollBar; affects Chat and Cursor

VideoConference

Creates a Flash client capable of displaying multiple instances of the AVPresence component

Automatically includes AVPresence

VideoPlayback

Provides a quick user interface to play back recorded streams

Nests no other components

VideoRecord

Records and publishes audio and video streams from a Flash movie to a FlashCom application

Nests no other components

Whiteboard

Displays a shared whiteboard with drawing and text tools

Nests no other components


2.1.1. Server-Side Requirements

Every communication component requires one or more Server-Side ActionScript (SSAS) files, which use the .asc extension. These files exist within the scriptlib folder of the installation directory for FlashCom and are automatically installed with Flash Communication Server MX. Fortunately, you can quickly enable communication components (i.e., make them available for use by your client-side applications) by loading the components.asc file into the main.asc document of any FlashCom application. The components.asc file loads other .asc files into the application instance to enable all of the communication components. Alternatively, you can load each component's .asc file individually to minimize the memory requirements of your FlashCom application.

Every communication component requires a client-side and server-side interaction. When you use a communication component instance in a Flash movie (the client), the component makes calls to a server-side component of the same name. For example, if you have a Chat component in your Flash movie, the component automatically creates a class named FCChatClass. When the Chat instance connects to the FlashCom application, the server-side application creates an FCChat class designed to communicate with the client-side FCChatClass instances.

You'll learn more about the component framework in Chapter 14. For the purposes of this chapter and its examples, we'll explore the basic functionality of the component framework with respect to the communication components.

To load the component framework, which enables client-side movies to use the communication components:

  1. Create a directory, such as myApplication, for your FlashCom application in the applications directory of your FlashCom installation.

  2. Use a text editor such as the one in Flash MX Pro 2004 to create a text document containing the following code:

    load("components.asc");

  3. Save this document as main.asc, in the directory you created in Step 1.

You can continue to add Server-Side ActionScript code to the main.asc to implement functionality beyond that of the communication components. Later in this chapter, we'll create basic applications that use the code shown in the previous steps.

If you don't want to load the server-side code for all communication components into your FlashCom application, you can load just the server-side code for the specific components you use in your Flash movie. For example, to load the server-side Chat component file, use:

load("framework.asc");
load("components/chat.asc");

in the first lines of your main.asc document.


2.1.2. Common Methods of the Communication Components

If you edit any of the communication component symbols in Flash MX 2004, you will find several methods that are common to all components.

In the following exercises, we place all ActionScript in frame 1 of the Actions layer of a document. Create a new layer named Actions for the purpose of holding scripts, and place it at the top of the timeline.

To follow along with a sample component, place an instance of the ConnectionLight component onto the Stage of a Flash document, right-click (Windows) or Ctrl-click (Mac) the instance, and choose Edit. Select frame 1 of the Actions layer and open the Actions panel (F9).

2.1.2.1 init( )

The init( ) method prepares, or initializes, each component instance with name and prefix properties as well as other properties specific to the component type. The prefix is a string used to help identify each component instance within the server-side component framework of the FlashCom application. Each instance is assigned a prefix property using the following convention:

class name + "." + instance name + "."

For example, an instance of the ConnectionLight component named connLight_mc using the Properties panel would have the prefix string value:

FCConnectionLightClass.connLight_mc.

Each component instance is also given a name property, which is exactly the same as the _name property of the movie clip instance for that component. If a component instance was not named in the Properties panel, the name property is set to the string value "_DEFAULT_".

2.1.2.2 connect( )

The connect( ) method of each communication component establishes a connection for the component instance to the FlashCom application. This method requires one argument: a NetConnection object that has already successfully connected to a FlashCom application. That is, the component piggybacks onto the previously established connection. For a component specified within the list of communication components for a SimpleConnect instance, its connect( ) method is called automatically. See Section 2.6 later in this chapter for details on calling connect( ) manually.

The connect( ) method uses the prefix property established in the init( ) method (discussed earlier) to invoke the server-side connect( ) method for the same component class. The server-side code is invoked from the client with a NetConnection.call( ) method, passing the prefix value and the "connect" method name as a string, along with any other necessary parameters for the communication component. Each component requires its connect( ) method to be called in order to initialize the new component within the FlashCom application.

We don't use the term connection here to imply an actual NetConnection instance connection or a user connection to the FlashCom application. Even though you may have several communication component instances in a Flash movie, all of the components can be connected to the FlashCom application via one NetConnection instance. A communication component's connection simply establishes one or more remote shared objects (RSOs) or streams associated with the component's functionality.


2.1.2.3 close( )

Each communication component's connection to the FlashCom application can be terminated with the close( ) method. This method invokes the server-side close( ) method for the component's class, using the same NetConnection.call( ) methodology discussed for the connect( ) method. Note that the communication component's close( ) method does not invoke the NetConnection.close( ) method from the Flash client; a connection to the FlashCom application is still maintained even though the component instance has closed its connection.

2.1.2.4 onUnload( )

The onUnload( ) method of each communication component simply invokes the same close( ) method discussed in the previous section. The onUnload( ) handler is invoked when a component instance is removed from the Stage of the Flash movie. An instance can be removed by the act of the playhead going to a frame where the instance does not exist or by client-side ActionScript code using the MovieClip.removeMovieClip( ) method.

2.1.2.5 setUsername( )

Many of the communication components, such as AVPresence and PeopleList, contain a setUsername( ) method. This method specifies the user's name as a string and is usually displayed somewhere in the user interface of the component. For example, if an AVPresence component instance named user_1_mc invokes the setUsername( ) method with a string of "Alan", the name appears at the top left of the AVPresence instance when the user begins to stream audio and video. The following syntax demonstrates this functionality:

user_1_mc.setUsername("Alan");