This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
114
|
Chapter 4: Applications, Instances, and Server-Side ActionScript
the one included in Flash MX Professional 2004 (Flash Pro). SSAS source files almost
always have the extension .asc (although .js is another option); SSAS is in fact
JavaScript 1.5.
When double-byte characters are required, such as for Kanji, you must
use a text editor capable of handling UTF-8 encoding. In addition,
method names containing higher-order double-byte characters must
use the array operator instead of the dot operator. For example:
obj = {};
obj.myMethodName = function ( ) {
trace("myMethodName");
};
obj["myMethodName"]( ); // Correct
obj.myMethodName( ); // Correct
obj[" "] = function ( ) {
trace(" ");
};
obj[" "]( ); // Correct
obj. ( ); // Syntax Error !!
Instances and Resources
FlashCom can run multiple instances of an application at the same time. Each
instance has its own memory, disk, stream, and shared object environment and runs
its own single-threaded copy of the main.asc script. Instances are an important way
to group users and partition the server’s resources among them. Perhaps the sim-
plest example of this is a chat application. Many instances of a chat application can
be run simultaneously—each with different users connected to it—effectively creat-
ing a set of chat rooms. Users who connect to rtmp:/courseChat/room1 can commu-
nicate with each other using the streams and shared objects associated with the
room1
instance. Other users who connect to rtmp:/courseChat/room2 can communicate
among themselves using the
room2 instance but will normally be unaware of the con-
versation occurring in the
room1 instance. If instances must share streams or shared
objects, one instance can make a network connection to another to gain access to its
resources.
Although you can build an application in which a single instance pro-
vides a lobby and multiple chat rooms simultaneously, it’s not recom-
mended. If a large number of users simultaneously use a single
instance, performance suffers. Because an instance’s ActionScript runs
in a single thread, every remote method call on the server, including
the delivery of chat messages, will execute sequentially.
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Scripting Application Instances
|
115
It is difficult to give precise limits on how many clients are too many for one instance
because each application makes different demands on the server. An instance that
makes extensive use of Server-Side ActionScript may support fewer than 20 clients
with adequate performance, while other applications may support hundreds or even
a few thousand. Chapter 16 covers architectures that scale more effectively.
The streams and shared objects available to each instance are located by FlashCom
using a relative URI. Most often this is just the name of the stream or shared object.
For example, if a Flash movie plays a stream named
intro, then “intro” is a relative
URI to the instance to which the movie is connected. If the instance URI is:
rtmp://my.university.edu/courseLectures/algebra101
then the full URI to the
intro video would be:
rtmp://my.university.edu/courseLectures/algebra101/intro
In practice, a full URI like that is never used, but it is useful to think of stream and
shared object resources as existing in a space defined by full URIs. We’ll make use of
full URIs later in this chapter to show how streams are organized within an applica-
tion. A Flash movie requests resources associated with an application instance using
a relative URI after a connection has been attempted. For example, a movie must
first request a connection to an instance before trying to publish or play a stream or
get a remote shared object:
nc.connect("rtmp://my.university.edu/courseLectures/algebra101");
Once the connection has been attempted, the Flash movie can request that a
recorded or live stream named
intro be played:
ns = new NetStream(nc); // Create a NetStream within a NetConnection.
videoArea.attachVideo(ns); // Attach the stream to a video object on the Stage.
ns.play("intro"); // Play a stream named intro.
Similarly, a shared object named streamList, which contains a list of stream names,
may be available for the
algebra101 instance. After a connection is requested to the
algebra101 instance, access to the streamList shared object could be requested as fol-
lows:
so = SharedObject.getRemote("streamList", nc.uri, true);
so.onSync = function (list) {
trace("onSync> list.length: " + list.length);
};
so.connect(nc);
The NetStream and SharedObject classes are described in detail in Chapters 5 and 8.
In both previous examples, the name of the stream and the shared object are simple
relative URIs. The relative path to a stream or shared object within an instance can
include directory-like names that help to separate resources into groups. For exam-
ple, lectures can be grouped by subject so that a relative URI to a video stream might

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.