XMLSocket Class

 <  Day Day Up  >  

XMLSocket Class

The XMLSocket class enables you to open a continuous connection with a server. A socket connection enables the server to publish (or "push") information to the client as soon as that information becomes available. Without a continuous connection, the server must wait for an HTTP request. This open connection removes latency issues, and it is commonly used for real-time applications such as mail, stock tickers, and news feeds. The data is sent over the socket connection as a single string and should be in XML format. The XML class is used to structure the data for all communication.

There are server-side requirements for using XML sockets in Flash. The server must have a listening application that is waiting for the Flash application to notify or register as interested in receiving data. After initial contact has been made between the Flash movie and the server, the server application can send messages to the movie. There are many different server-side technologies that can support socket communication. For instance, Java is a common programming language used for server-side sockets.

How to Use a Socket

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 to a web server port. The send() method passes an XML object to the server specified in the socket connection.

Listing 12.19 is an example of creating a socket class named XMLComm . Note that we have subclassed the XMLSocket class to create the socket. The custom socket class opens a connection in the constructor. When the connection has been successfully established, data can be transmitted to the server. By implementing the onXML() method, the object is capable of receiving messages from the server.

Listing 12.19. Custom XMLSocket Class
 class XMLComm extends XMLSocket{       var serverName:String;       var port:Number;       public function XMLComm(serverName, port) {             super();             this.serverName = serverName;             this.connect(serverName, port);       }       // Define onConnect() method  that handles       // the server's response. If the connection succeeds, send the       // XMLData object. If it fails, provide an error message       private function onConnect(success){         if (success){             this.send(XMLData);         } else {             trace("There has been an error connecting to             "+this.serverName);         }       }       private function onXML(XMLObj){         //This is invoked when XML has been received from the server         //parse the data and display in the client       } } 

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:

  • The close() method of the XMLSocket class is called.

  • No more references to the XMLSocket object exist.

  • Flash Player exits.

  • The connection is broken (the network fails).

Socket Security

The XMLSocket.connect() method can connect only to TCP port numbers greater than or equal to 1024. One consequence of this restriction is that the servers that communicate with the XMLSocket object must also be assigned to port numbers greater than or equal to 1024. Port numbers below 1024 are often used by system services such as FTP, Telnet, and HTTP; thus, the XMLSocket object is barred from these ports for security reasons. The port number restriction limits the possibility that these resources will be inappropriately accessed and abused.

 <  Day Day Up  >  

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net