Using the XML Socket Object


Before you can connect Flash to a socket server, you must create a new XMLSocket object. You can then use the methods of that object to connect to a server and exchange information. In this section, we'll show you how to create and use an XMLSocket object while also using the XML object methods and properties introduced earlier in this lesson.

To create a new XMLSocket object, you must use the constructor for XMLSocket. Here's an example:

 server = new XMLSocket();  

The above line of ActionScript creates a new XMLSocket object named server. To connect the XMLSocket object to a server, you simply employ the connect() method of the XMLSocket object using the following syntax:

 server.connect(hostName,port)  

The hostName parameter is the IP address on which the socket server resides usually a numeric sequence (for example, 65.134.12.2). Since you'll be connecting to your own machine in this exercise, you can use either the sequence "127.0.0.1" or "localhost." Both 127.0.0.1 and localhost are valid references to your own computer. If you were to type http://localhost in your Web browser's address bar, the browser would try to connect to your computer as if it were a Web site. The port parameter refers to the port on which the server is listening. Flash can only connect to ports higher than 1024. For example,

 server = new XMLSocket();  server.connect("localhost", 9999) 

You can close a connection with a socket by using the close() method, which looks like the following:

 server.close();  

To send information via the socket connection, simply use the send() method and pass in the object you wish to send. For instance,

 server.send("<Text>Hi</Text>");  

The XMLSocket can respond to the following types of events:

onConnect

This event fires when the connection is accepted or when it fails.

onXML

This event fires when information arrives via the socket connection. This lets you know that new information has arrived.

onClose

This event fires when the connection with the socket is lost.

As we did with the onLoad event in the XML object, we have to define these event handlers with the XMLSocket we create.

For example,

 function serverConnected (success) {    trace(success); } server.onConnect = serverConnected; 

The serverConnected() function is called when the onConnect event is fired. The success parameter, shown in the function definition, has a value of true if the connection was successful; false if it wasn't.

The onXML event is used as follows:

 function xmlReceived (data) {    trace(data); } server.onXML = xmlReceived; 

The xmlReceived() function is called each time information arrives via the socket. The data parameter contains the XML document pushed into Flash.

The onClose event handler can be defined and used as follows:

 function socketClosed () {         //notify the user } server.onClose = socketClosed; 


Macromedia Flash MX Game Design Demystified(c) The Official Guide to Creating Games with Flash
Macromedia Flash MX Game Design Demystified: The Official Guide to Creating Games with Flash -- First 1st Printing -- CD Included
ISBN: B003HP4RW2
EAN: N/A
Year: 2005
Pages: 163
Authors: Jobe Makar

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