ADOBE FLEX 3
Developer Guide
475
Save the document to your hard disk as SimpleServer.java and compile it using a Java compiler, which creates a Java
class file named SimpleServer.class.
You can start the XMLSocket server by opening a command prompt and typing
java SimpleServer. The
SimpleServer.class file can be located anywhere on your local computer or network; it doesnt need to be placed in
the root directory of your web server.
If youre unable to start the server because the files are not located within the Java classpath, try starting the server with
java -classpath . SimpleServer.
To connect to the XMLSocket from your ActionScript application, you need to create a new instance of the
XMLSocket class, and call the
XMLSocket.connect() method while passing a host name and port number, as
follows:
var xmlsock:XMLSocket = new XMLSocket();
xmlsock.connect("127.0.0.1", 8080);
A securityError (flash.events.SecurityErrorEvent) event occurs if a call to XMLSocket.connect() attempts to
connect either to a server outside the caller’s security sandbox or to a port lower than 1024.
Whenever you receive data from the server, the
data event (flash.events.DataEvent.DATA) is dispatched:
xmlsock.addEventListener(DataEvent.DATA, onData);
private function onData(event:DataEvent):void
{
trace("[" + event.type + "] " + event.data);
}
To send data to the XMLSocket server, you use the XMLSocket.send() method and pass an XML object or string.
Flash Player converts the supplied parameter to a String object and sends the content to the XMLSocket server
followed by a zero (0) byte:
xmlsock.send(xmlFormattedData);
The XMLSocket.send() method does not return a value that indicates whether the data was successfully trans-
mitted. If an error occurred while trying to send data, an IOError error is thrown.
Each message you send to the XML socket server must be terminated by a newline (
\n) character.
Storing local data
A shared object, sometimes referred to as a “Flash cookie,” is a data file that can be created on your computer by the
sites that you visit. Shared objects are most often used to enhance your web-browsing experience—for example, by
allowing you to personalize the look and feel of a website that you frequently visit. Shared objects, by themselves,
cant do anything to or with the data on your computer. More important, shared objects can never access or
remember your e-mail address or other personal information—unless you willingly provide such information.
New shared object instances can be created using the static
SharedObject.getLocal() or
SharedObject.getRemote() methods. The getLocal() method attempts to load a locally persistent shared object
that is available only to the current client, whereas the
getRemote() method attempts to load a remote shared object
that can be shared across multiple clients by means of a server, such as Flash Media Server. If the local or remote
shared object does not exist, the
getLocal() and getRemote() methods will create a new SharedObject instance.
The following code attempts to load a local shared object named
test. If this shared object doesn’t exist, a new
shared object with this name will be created.

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 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.