User Guide
390 Networking and Communication
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 transmitted. If an error occurred while trying to send data, an IOError error is
thrown.
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, can’t 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.
TIP
Each message you send to the XML socket server must be terminated by a newline (\n)
character.