Connecting with External Sources > Sending and loading variables to and from a remote source > Using the XMLSocket object |
![]() ![]() ![]() |
Using the XMLSocket object
ActionScript provides a built-in XMLSocket object that allows you to open a continuous connection with a server. A socket connection allows the server to publish (or "push") information to the client as soon as that information is available. Without a continuous connection, the server must wait for an HTTP request. This open connection removes latency issues and is commonly used for real-time applications such as chats. The data is sent over the socket connection as one string and should be in XML format. You can use the XML object to structure the data.
To create a socket connection, you must create a server-side application to wait for the socket connection request and send a response to the Flash movie. This type of server-side application can be written in a programming language such as Java.
You can use the ActionScript XMLSocket object's connect
and send
methods to transfer XML to and from a server over a socket connection. The connect
method establishes a socket connection with a Web server port. The send
method passes an XML object to the server specified in the socket connection.
When you invoke the XMLSocket object's connect
method, the Flash Player opens a TCP/IP connection to the server and keeps that connection open until one of the following happens:
![]() |
The |
![]() |
No more references to the XMLSocket object exist. |
![]() |
The Flash Player quits. |
![]() |
The connection is broken (for example, the modem disconnects). |
The following example creates an XML socket connection and sends data from the XML object myXML
. To understand the script, read the commented lines (indicated by the characters //
):
// Create a new XMLSocket object sock = new XMLSocket(); // Call its connect method to establish a connection with port 1024 // of the server at the URL sock.connect("http://www.myserver.com", 1024); // Define a function to assign to the sock object that handles // the server's response. If the connection succeeds, send the // myXML object. If it fails, provide an error message in a text // field. function onSockConnect(success){ if (success){ sock.send(myXML); } else { msg="There has been an error connecting to "+serverName; } } // Assign the onSockConnect function to the onConnect property sock.onConnect = onSockConnect;
For more information, see XMLSocket (object) in the ActionScript Dictionary.
![]() ![]() ![]() |