![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
ActionScript provides a built-in XMLSocket class 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 class 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 SWF file. This type of server-side application can be written in a programming language such as Java.
You can use the connect()
and send()
methods of the XMLSocket class 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 connect()
method, Flash Player opens a TCP/IP connection to the server and keeps that connection open until one of the following happens:
close()
method of the XMLSocket class is called.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 class.
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |