ActionScript and the Programming Model

 <  Day Day Up  >  

ActionScript is the driving force behind Flash Communication Server MX. The prebuilt ActionScript classes and SSAS objects offer an excellent event-based model. It is a model that lets you quickly develop, test, and deploy communication applications. Flash Communication Server MX is one great big class hierarchy. From the server, to the application, and right down to the user ”everything about the server is object-oriented programming (OOP) and good.

Let's take a look at what makes Flash Communication Server MX tick, specifically some of the key elements that make up the server:

  • Persistent connection

  • Flash Communication ActionScript objects

  • SSAS

  • Streaming

  • Remote SharedObjects

  • Information objects

  • Messaging

  • The Flash Communication Server MX component framework

Let's look at these elements in detail.

Persistent Connection

RTMP was developed by Macromedia specifically for Flash Communication Server MX applications. It's a persistent communication method that's responsible for transporting AMF packets between client and server. AMF is the same format used in Flash Remoting. The key difference is that RTMP has no disconnection, which gives the developer an opportunity to push data to the client whenever required.

RTMP can operate over any port on your system, including port 80, usually reserved for HTTP-based web servers. As a default, Flash Communication Server MX is set to communicate using ports 1935 (public access) and 1111 (administrator access). Flash Communication Server MX and HTTP can interoperate on port 80 using a tunneling technique, but it is strongly recommended that a different interface be used to operate Flash Communication Server MX on port 80.

It takes only two lines of ActionScript to connect to Flash Communication Server MX. One creates an instance of the NetConnection class, and one connects the instance to the server:

 var my_nc:NetConnection = new NetConnection(); my_nc.connect("rtmp://localhost/myApp/myInstance"); 

You can make this code as dynamic and complex as you need to. Note that in ActionScript 2.0, Macromedia reengineered the NetConnection object into an ActionScript 2.0 class. This upgrade permits a progressive download used strictly to play FLV files without Flash Communication Server MX.

Flash Communication ActionScript Objects

When Flash Communication Server MX was first introduced, it made available a series of new programmable ActionScript objects within the Flash MX authoring environment. These ActionScript objects enabled you to access and control camera and microphone devices, let you persist data on a user's computer, and let you stream pre-recorded video; best of all, these new objects let you code a connection between the Flash Player and Flash Communication Server MX.

Flash MX Professional 2004 has upgraded versions of the original Flash communication ActionScript objects. They have been re-written as ActionScript 2.0 classes with significant performance improvements. These specific ActionScript communication objects include:

  • NetConnection . This object controls the persistent connection between Flash Player 7 and the Flash Communication Server. It is a multi-direction communication channel.

  • NetStream . This object manages live and pre-recorded video and audio streams. It manages publishing live streams from the Flash Player to Flash Communication Server MX. It also handles messaging over the stream to and from Flash Communication Server MX and other Flash Players.

  • SharedObject . This object manages persistent and temporary ActionScript objects on the Flash Communication Server. The ActionScript object doubles its duty by also handling local SharedObjects .

  • Camera . This object controls the video digitizing engine in Flash Player 7. Quality and video source are managed by referencing this object.

  • Microphone . This object, like the Camera object, controls the audio digitizing engine in Flash Player 7. Quality and microphone source are managed by referencing this object.


SSAS is the scripting language of Flash Communication Server MX. It offers the developer the opportunity to call methods and properties and to handle specific events related to communication applications.

SSAS, like Flash ActionScript, is a derivative of the ECMA Script-262 specification. It also is an invocation of ServerSide JavaScript (SSJS) used in Netscape's Enterprise server.

SSAS contains most of the core Flash ActionScript objects. It contains no movie-oriented objects. SSAS can be developed within Flash using the Integrated ActionScript window. Like ActionScript 2.0, SSAS is case-sensitive. All scripting is done inside or loaded into the file main.asc.

The main.asc file is the class for the application. An application runs from an instance of the main.asc file. When SSAS is modified, each instance of the application must be reloaded for the change to take effect.

Application instances are completely independent of each other. They cannot share data or streams between them without a crossover proxy connection. This operation ensures the security of communication within the instance.

There are five key SSAS objects available to developers. They are: Application , Client , NetConnection , Stream , and SharedObject .

Application and Client objects are unique to SSAS. The SSAS objects, NetConnection , Stream , and SharedObject , share similar qualities to the Flash ActionScript counterpart , with some variation on invocation.

The Application SSAS object is built on four key system events. These events are invoked at four different times. The first is when the application is started (instantiated). The second is when a connection is requested (client is instantiated ). The third is when the client disconnects (instance is destroyed ). The last is when the application is stopped or shut down (application instance is destroyed). These events are the foundation for all Flash Communication Server MX-to-client communications.

A simple example of an application event developed using SSAS is as follows :

 application.onConnect = function(my_client, args) {       application.acceptConnection(my_client)       trace(args);       /* Some SSAS Statements */ } 

The Client SSAS object is used to manage the communication between a single client and Flash Communication Server MX. The object is uniquely instanced each time a client makes a connection request. It is destroyed when the client is disconnected or rejected by the server. New functions defined within the Client object instance can be called by the remote client with a procedure called messaging. This process is used to call server functionality from the client, or client functionality from the server. The Client object also stores information about the connection, including bytes sent and received and connection speed.

The NetConnection SSAS object is similar to the Flash ActionScript object. In SSAS, it is used to proxy, or chain, servers together to redistribute data or streams. This process increases the hardware and licensing capacity of Flash Communication Server MX.

The SharedObject and Stream SSAS objects are very similar to their Flash ActionScript counterpart. A SSAS SharedObject makes reference to a persistent or temporary Remote SharedObject on the local server or on another server through a proxy connection. The Stream SSAS object is like the Flash ActionScript object, NetStream . Although there are some key differences to the way methods are used, the object is fundamentally the same as its Flash ActionScript counterpart.

SSAS can also interact with databases using Flash Remoting. The syntax for Flash Remoting in SSAS is exactly the same as you would use in Flash ActionScript. For more information on Flash Remoting, search for "Flash Remoting" at


Flash Communication Server MX has become known as a powerful streaming engine. With the Flash Player having such a large install base, it becomes a great option for companies to stream live and pre-recorded video and audio.

To stream a prerecorded video or MP3 from Flash Communication Server MX, you need only place the FLV or MP3 file on the server, and then place these ActionScript 2.0 statements in your Actions panel:

 var my_ns:NetStream = new NetStream(NetConnection_nc);"myStream"); /* Attach the stream to a video object */ my_video.attachVideo(my_ns); 

The first statement creates an instance object of the NetStream class and the second commands the instance to start playing a prerecorded video stream called myStream.flv . The NetStream class has been reengineered from the ActionScript 1.0 NetStream object. It is 100 percent backward-compatible with Flash Player 6; however, when used with Flash Player 7, it supports progressive HTTP download for the FLV format.

Remote SharedObjects

You can use the local SharedObject to store persistent ActionScript objects and data on a user's hard disk. The RSO programming model lets you store the same ActionScript objects on a remote server. If that wasn't exciting enough, these ActionScript objects can be shared between other remote Flash Players.

The basic ActionScript to connect with an RSO is also no more than these two statements:

 var my_so:SharedObject = SharedObject.getRemote("clientSO", my_nc.uri, true); my_so.connect(my_nc); 

The first statement instances the SharedObject class into a local variable. This is a key statement that identifies what RSO to connect to and where it is. The statement also identifies the persistence of the RSO. The second statement establishes the connection to the (already connected) NetConnection .

Writing data to a Remote SharedObject is simple. SharedObjects store your ActionScript 2.0 objects as a slot within the data property. Here's a sample statement that stores an array in a Remote SharedObject :["slotName"] = easy_array; 

Reading data from a Remote SharedObject is equally as simple:

 var myLocal_array:array =["easy_array"]; trace("the value of element 0 is: " + myLocal_array[0]); 

Figure 15.4 shows the structure of the Remote SharedObject . Notice how it stores the easy_array and product_array as slots within the data property. See Figure 15.5.

Figure 15.4. Slots can contain ActionScript objects such as Arrays and Arrays of Objects.


Figure 15.5. The SharedObject structure seen in Figure 15.4 is created with this ActionScript.


The real power of RSO is the synchronization engine. Flash Communication Server MX monitors every slot in the SharedObject and pushes messages to every client connected to the RSO. It will even inform the client changing the data if the change was successful.

For example, if any value in easy_array (see Figure 15.5) is changed or removed, Flash Communication Server MX sends a message that informs all clients to update their reference to the array. The structure development of slots is important to ensure that a conservative approach to data transfer is achieved.

Information Objects

Information objects contain messages for ActionScript that detail status changes or errors occurring with specific object instances. They are as follows:

  • NetConnection . Information objects inform the player of the connection status and any errors with the connection or messages traveling over the connection. For example, when an information object is returned with a connection error, you could script a reconnect attempt or do something in the UI.

  • NetStream . This informs the client on the status of streaming objects being played from Flash Communication Server MX or published to Flash Communication Server MX. This is an important object to monitor to ensure the user can see the video as it should be.

  • Camera . and Microphone . These objects inform the player if the user has muted ( denied access to) the device. The user can do this by using the Flash Player context menu.

  • SharedObjects . These have two types of information objects. The status object returns information about the connection and message status of the object. The sync object contains detailed information about a slot when it changes. The sync object should always be monitored closely, and ActionScript statements handle the sync notifications.

Information objects are ActionScript objects with a collection of no more than four properties. Important properties include code and description . Most objects are returned to the onStatus event of the specific instance.

The synchronization objects used in RSOs are returned as an Array of Objects. A single information object can contain change notifications about multiple slots. This is usually the case when a client reconnects to the object and an initial resync takes place.


Messaging is another key function of Flash Communication Server MX. Because Flash Player can share a stream or RSO, ActionScript can also send messages to invoke special handlers listening on the stream. Because the Flash player and Flash Communication Server MX share an intimate connection, either end can call a remote function and have an ActionScript object returned. There are two methods of messaging within Flash Communication Server MX: a broadcast send or a private, asynchronous call.

Broadcast Send

A broadcast send lets a Flash Player or the Flash Communication Server MX call a function handler on other Flash Players or Flash Communication Server MX. It is a one-way blast to every client that is listening.

Broadcasting messages are sent over a NetStream instance or a Remote SharedObject , each using the send() method. In the case of NetStream , only Flash Communication Server MX or the publishing Player can send a message. Only clients that are subscribed to (playing) the stream can receive the message.

A simple broadcast send might look like this in Flash ActionScript 2.0:

 publish_ns.publish("someLiveStream", "record"); publish_ns.send("remoteFunction", "A message or AS object"); 

The NetStream handler on Flash Players subscribing to the stream would look like this:"someLiveStream"); play_ns.remoteFunction = function(args) {       // Some ActionScript statement... } 
Asynchronous Call

Asynchronous call is a private messaging channel between Flash Communication Server MX and the client. It operates very much like Flash Remoting. It is asynchronous because it will respond back to the caller; however, it won't happen instantaneously, as when calling a local function. Network congestion and transport slow the operation from returning objects back to the caller.

There are two key differences between this method and the broadcast send: asynchronous is two-way messaging and it is private between the client and the server.

A simple asynchronous call from the Flash Player to the server would look like this:"serverFunction", new resultObject, "A message or object"); 

The call sends the objects returned in the call back to a new instance of the resultObject . This is a custom constructor with an onResult event handler. The onResult event handler will be called when the server responds, like so:

 resultObject = function() {       this.onResult= function(retObject) {             /* ActionScript Statement handling the callback */       } } 

The server handler extends the Client SSAS object. This way, each time a new connection is received, a Client instance is automatically created with this event handler:

 Client.prototype.serverFunction = function(args) {       /* Some SSAS statements */       return "hello from the server, your IP is: " + this.ip; } 

An asynchronous call occurs between the NetConnection instance (on the client) and the Client instance (on Flash Communication Server MX).

Flash Communication Server MX Component Framework

Macromedia did not leave Flash Communication Server MX developers alone without giving them a collection of visual UI components specifically for use with Flash Communication Server MX. The UI component set includes basic components for connecting the Flash player to Flash Communication Server MX. It also contains a simple chat component, a video conferencing component, a synchronized presentation component, and a video playback. Figure 15.6 shows the full Library of prebuilt communication components available.

Figure 15.6. The Flash UI components offer a quick way to get up and running with Flash Communication Server MX.


The most interesting part of these components, from an ActionScript perspective, is that they deploy an ActionScript framework both in SSAS and in Flash ActionScript. This is very useful for both extending the prebuilt components and building your own.

 <  Day Day Up  >  

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162 © 2008-2017.
If you may any questions please contact us: