Accessibility

Table of Contents

Using Remote Methods with Flash Media Server

A Simple Chat Application

One of the most common (or should I say most obvious) applications that you can build almost exclusively on top of SharedObject.send is a simple chat application. Let's take a closer look at one (see Figure 1). Instead of recreating the complete application for this example, I will just zoom in on the important parts.

Simple chat application

Figure 1. Simple chat application

Note: This example assumes that you have installed Flash Media Server on your local machine and that it is running. You also need to create a new application folder called simplechat and copy the file main.asc from the sample download's simplechat folder into it (see Figure 2). For more information about setting up applications on Flash Media Server, see the Flash Media Server documentation.

Copying main.asc into the simplechat application folder in FMS 2

Figure 2. Copying main.asc into the simplechat application folder in FMS 2

Open simplechat.fla as well as SimpleChat.as in the Flash authoring environment. SimpleChat.as is a class file linked to the simplechat movie clip on the Stage. Let's examine the code in more detail and, in particular, the usage of SharedObject.send.

If you scroll down to line 75, you will find the connectSO method, which sets up the remote shared object once NetConnection has been established to the server.

Line 102 sets up a method called chatMsg on the shared object:

_chatSO.chatMsg = function (  msg, fromwho )

This method is the one that is being called with SharedObject.send when a user submits a chat message and it accepts two parameters, msg and fromwho. The msg parameter contains the actual chat message, while fromwho indicates who sent the message.

The code that follows simply formats this text and pushes it into a text area on the Stage:

var chatmessage = "<FONT FACE='Verdana' SIZE='11'><b>" + fromwho + "</b>: " + msg + "</FONT>";
history_ta.text += chatmessage;
// scroll down
history_ta.vPosition = history_ta.maxVPosition;
history_ta.redraw();

Line 130 contains the actual SharedObject.send statement which, when called, invokes _chatSO.chatMsg on all connected clients passing it the msg and fromwho parameters:

_chatSO.send("chatMsg", msg, fromwho);

Feel free to modify this application further—maybe by adding additional parameters to the _chatSO.chatMsg method that will allow you to target a specific user.

One thing to remember is this: SharedObject.send is ideally suited to inform all connected users easily about a specific event. The server too can invoke SharedObject.send; the request does not have to originate from a client.