Accessibility
 
Home / Developer Center / Director Developer Center /

Director Article

Icon or Spacer Icon or Spacer Icon or Spacer
John Taylor
John Taylor
www.janza.com
 
Director MX and Macromedia Flash Communication Server MX


Until recently, most developers used Shockwave Multiuser Server to create multiuser applications with Director. Now there's a new kid on the block—Macromedia Flash Communication Server MX.

In this article, I'll look at some of the strengths of Macromedia Flash Communication Server MX and then dive into an example of using it with Director. Along the way, I'll talk about areas where Macromedia Flash Communication Server MX is similar to, or different from, Shockwave Multiuser Server.

 

Why use Macromedia Flash Communication Server MX?
Macromedia Flash Communication Server MX is a whole new product, built from the ground up. It's not a direct descendant of Shockwave Multiuser Server, and there are a few things that Flash Communication Server can't do. If you need UDP (User Data Protocol) or peer-to-peer features, you're better off using last year's model (Shockwave Multiuser Server).

But many of the core Shockwave Multiuser Server features have made it to Flash Communication Server—for example, client-server messaging and server-side scripting. The syntax for these features is different but the basic functionality is the same.

So what's new about Flash Communication Server? The most obvious additions are video and audio. These definitely have the wow-factor going for them. But there's more to Macromedia Flash Communication Server MX than video-conferencing.

Macromedia Flash Communication Server MX is designed to support a heavier user load than Shockwave Multiuser Server. It's been tested successfully with tens of thousands of simultaneous connections. Administration features are improved, including prebuilt admin tools and an API for accessing server states and statistics. Macromedia Flash Communication Server MX also supports Macromedia Flash Remoting, which opens up new possibilities for connecting to application servers such as .NET and ColdFusion.

Macromedia Flash Communication Server MX also ships with several drag-and-drop components. Components are like behaviors and they make it easy to add communication features—such as shared whiteboards, video, and chat—to your applications:

 
Communication components
 
Just configure the components in a Macromedia Flash movie and put the movie in the Director MX score.
 

Last but not least is a new feature called SharedObjects. This is one of my personal favorites. In the following sample application, you'll take a look at SharedObjects, client-server messaging, and other details of using Macromedia Flash Communication Server MX in Director.

A sample application
In this sample application, each user gets an avatar (icon)—in this case, a fish. You can change the size, speed, and rotation of your fish, and your changes will appear on everyone else's screen. The functionality is intentionally simple to make it easier to focus on the code itself.

Here's what the screen looks like with several users swimming around:

 
Fish in action
 
Getting started   To get the sample application working, you'll need to install Macromedia Flash Communication Server MX or have access to a machine that already has Macromedia Flash Communication Server MX. As an alternative, you can sign up with one of the hosting companies that support Macromedia Flash Communication Server MX.

You will also need to download the source code files:

Download the components fish.zip (516K)
Download the components fish.sit (476K)

The server side   Start with the server side of the application. Copy the file called main.asc to the following location on the server:

/Flash Communication Server MX/flashcom/applications/fish/main.asc

All that an application really needs to get started on the server side is a folder with a unique name. The main.asc file can be empty, or you can skip it altogether. In this case, however, main.asc contains server-side ActionScript to help manage the Fish application. The first function, called onAppStart, executes when the application starts up (usually when the first user enters).

Note the line below:

application.users = SharedObject.get("users", false);

Here, you're creating a SharedObject to keep track of all users in the application. You'll look more closely at SharedObjects in a moment.

The next function in the Fish server-side script is called application.onConnect(). As the name implies, this function gets called every time a new user connects to the application. There's a lot going on in this function, but let's look at a few important lines of code.

In the first line of the function, you can see three arguments:

application.onConnect = function(client, username, userinfo)

The first argument, the client object, gets passed to the connect function automatically. The next arguments, username and userinfo, are custom arguments that you're passing from the Director Lingo code on the client. This is one way to get information to the server even before the connection has been accepted.

Next, create a unique number and ID for the user. This information gets returned to the user for identification purposes:

// Call the client to tell them what their user ID is.
var vbSuccess = client.call("setUserNumber", 0, userNumber);

Notice that this is a simple example of server-client messaging. It might come in handy if you generally created similar calls with Shockwave Multiuser Server.

Finally, create a SharedObject for each user:

userSO = SharedObject.get(userid, false);

Use this SharedObject to keep track of information for each user. The parameter "false" tells the server not to keep this object around after the user disconnects.

What's so great about SharedObjects?
A SharedObject is a container for data. Each SharedObject can have many named slots that carry information. When data in a slot changes, the server notifies all clients who have subscribed to that SharedObject of the change.

It seems like such a simple concept but it's very useful in a multiuser setting. You don't have to compose a message describing the change and then send it to everyone in a group. You just change the value of the data in the SharedObject.

SharedObjects in Macromedia Flash Communication Server MX are similar to group attributes in Shockwave Multiuser Server. But SharedObjects remove some of the complexities of group attributes, add new functionality of their own, and are generally easier to work with in your application.

Each user in this application will have their own SharedObject, which describes their fish. The SharedObject will have several named properties (or slots) including speed, scale, and rotation. When a client movie updates one of these slots, all other clients will be notified automatically:

 
Changing data slots using the Fish control panel
 

The client side
Now it's time to open the Director movie called fish.dir. Almost all of the code on the client side is written in Lingo. The first thing you'll notice on the Stage is a field that contains the following URI:

rtmp://www.your_fcs_domain.com/fish

Change this address to match the domain name of your Flash Communication Server.

Rooms and application instances
Note the word "fish" at the end of the URI above. This tells Macromedia Flash Communication Server MX that you want to run the Fish application and you want to join the default instance for this application. In classic multiuser environments, an instance is sometimes called a "room." Users often meet in a "lobby" and then pair off into rooms to meet with a smaller subset of users.

This is another area where Macromedia Flash Communication Server MX has evolved a bit from the previous Shockwave Multiuser Server architecture. To make a room with Macromedia Flash Communication Server MX, you don't have to create groups and send different "joinGroup" messages to different users. Instead, you just add a word to the end of the application URI.

In other words, this room:

…fish/PacificOcean

would be a different instance of the Fish application than this room:

…fish/AtlanticOcean

Both rooms will launch a new instance of the same server-side script (main.asc) and all variables in the script will be specific to their instance. This simplifies the process of setting up rooms and partitioning users into different groups.

Tip about URIs: Some administrators set up Macromedia Flash Communication Server MX to deny connections from addresses that don't match the domain of the server. This is for security reasons and it's a common practice with hosting companies. Unfortunately, it makes development and debugging more difficult because you have to upload the movie to the server to test it. If you find that you can't connect to Macromedia Flash Communication Server MX from your local machine, ask the administrator to add your IP address to the "allow" section of Macromedia Flash Communication Server MX setup.

Creating a connection
Whereas all Shockwave Multiuser Server calls are piped through the Multiuser Xtra, Macromedia Flash Communication Server MX uses Macromedia Flash Player as the conduit for calls between the client and the server. You can create the connection by using a Macromedia Flash sprite or by creating a global Macromedia Flash object. In this sample application, you'll be using a Macromedia Flash sprite.

Note: Be sure that the Macromedia Flash sprite is within the visible boundaries of the Director Stage or else the sprite won't get the servicing required to receive callbacks from the server. This Macromedia Flash sprite doesn't contain any graphics so I've hidden it from the user by setting the ink to background transparent.

Most of Macromedia Flash Communication Server MX Lingo for Fish is in a script member called "Flashcom behavior." The first step in Macromedia Flash Communication Server MX (as in Shockwave Multiuser Server) is to create a connection. This happens in the method called "connectToServer":

on connectToServer (me, aUri, aUsername)

  -- [snip: some init code].
  pConnection = pSprite.newObject("NetConnection")
  pSprite.setCallback(pConnection, "onStatus", #connectionStatusHandler, me)
  pSprite.setCallback(pConnection, "setUserNumber", #setUserNumber, me)
  pConnection.connect(aUri, aUsername)

end

Here you're using your Macromedia Flash sprite, pSprite, to create a connection object. Then you set two callbacks—a process that is very similar to setting netMessageHandlers using Shockwave Multiuser Server.

The first callback defines which method the server should call with any status messages about the connection itself. This will come in handy right away when the server calls back to tell you whether the connection has been accepted or rejected.

The second callback in this method is for setUserNumber. You may remember that the server-side script made a call to the client to send back a user ID. Now, you're using setCallback to route that message to the appropriate method on the client.

With this callback in place, you have a complete and simple example of sending messages from the server to the client. Making calls the other way is just as easy. I won't go into great detail here, but here's the basic syntax:

Lingo on the client:

pConnection.call("someMethod", "someArgument")

ActionScript on the server:

Client.prototype.someMethod = function(someArgument) {
    // Do something here.
}

You have created a connection and received your user ID from the server. Now you're ready to use this connection to pass data between all of the users in your application instance.

Subscribing to a SharedObject
You could use messages to pass data back and forth. This would be a familiar strategy if you were using Shockwave Multiuser Server. But for this example you'll use SharedObjects instead.

Your "Flashcom behavior" includes a method called newUser. This is where you subscribe to the SharedObject that represents each user (or each fish).

Subscribing to a SharedObject is a simple process. First you get the object and then connect to it:

tUserSO = tSharedObject.getRemote(("u" & tUserNumber), pConnection.uri, FALSE)
tUserSO.connect(pConnection)

The first SharedObject that you subscribe to holds the data for each user's own fish. After that, you subscribe to a new SharedObject every time a new user becomes known. This usually happens when the list of users changes. You can find the code for monitoring the user list in the method called syncUsers.

Using a SharedObject to receive data
When another user's fish changes (for example, when the fish increases its speed) your client movie is notified or "synced." You have used setCallback to direct these changes to the sprite that displays the corresponding fish for that user.

Open the script member called "User sprite." Inside you'll find a method called syncUserObject. This is where you process changes to the user's SharedObject.

When a SharedObject changes state, the event can be one of several code types. Let's look at the events with the code type "change." Look for this code type in the following lines, get the name of the data slot that has changed, and get the new value of that slot:

case tCode of
    "change", "success":
        tSlotName = symbol(tNextInList.name)
        tSlotValue = value("tData." & tSlotName)

Then use the new value to change the fish for that user. For example, if tSlotName is "speed" and tSlotValue is "10," tell the user's fish sprite to set its speed to 10.

When a slot changes, only the data for that slot is sent, rather than the entire SharedObject. If you're concerned about performance and bandwidth usage, you might want to keep this in mind when designing the structure of your data.

Using a SharedObject to send data
Now you'd also like to send data in the opposite direction so you can tell other users about changes that you make to your own fish. To do this, just change the appropriate slot on our SharedObject.

In the "Flashcom behavior," find this method:

on setUserProperty(me, aSharedObject, aSlotName, aSlotValue)

  tSOsetter = pSprite.getVariable("gSOsetter", FALSE)
  tSOsetter.setDataSlot(aSharedObject, aSlotName, aSlotValue)

end

SetDataSlot is a function in your Macromedia Flash sprite that can change any SharedObject that you pass to it. For example, you can flip the rotation of your fish by passing the fish's SharedObject to this method and including a slot name of "rotation" and a slot value of "180."

That's all you need to do to send new data. All of the other clients will receive the event on their end and change the rotation of your fish on their screen.

To see these events in action, open the message window and watch the "put" messages as they come in. You can also roll the mouse over any of the fish to see the name of the user that owns that fish.

Of course, there is more to Flash Communication Server than a school of fish avatars. But hopefully this article helps with some of the basic elements that you need to use Macromedia Flash Communication Server MX in Director MX.

Additional resources

  • Debugging and admin tools: Macromedia Flash Communication Server MX includes some excellent admin and debugging tools. The Communication App Inspector is the best place to start. You'll find it on the "Getting Started" page installed with the server:
 
The Communication App Inspector
 
    Among other things, you can embed trace messages in your server-side code and view the messages in the Communication App Inspector. Macromedia Flash also includes debugging tools that you can use from the client-side authoring environment.
 
  • Macromedia Flash Communication Server MX Development Center: Flash Communication Server has its own Application Development Center that includes lots of great articles about the server, components, Flash Remoting, and other topics. This is also the place to check for updates to the server and communication components.

  • Macromedia Flash Communication Server MX forums: Macromedia's Flash Communication Server forum covers a range of topics and experience levels. The Chattyfig FlashComm forum is frequented by more-advanced Macromedia Flash and ActionScript developers but can be a good source regardless of your level of experience.

  • ActionScript: It's worth checking out ActionScript if you haven't already. It doesn't take much to become productive, especially if you know some JavaScript. You won't need to use a lot of ActionScript from the client side in Director MX but a few lines of ActionScript on the server side can make a big difference in your Macromedia Flash Communication Server MX applications.
 

About the Author
John Taylor is the founder and managing director of Zingtime, Inc. He has more than 10 years of experience creating games and educational software for clients such as Electronic Arts, Lucas Learning, and the NFL. John led the design and development of the first multiuser games at shockwave.com and created many of Macromedia's PowerApp examples for Shockwave Multiuser Server. John studied software design at the MIT Media Lab and educational technology at the Harvard Graduate School of Education.