13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Sockets<br />

}<br />

}<br />

if(str.trim().equals("EXIT"))<br />

{<br />

done = true;<br />

}<br />

}<br />

incoming.close();<br />

}<br />

}<br />

catch (Exception e)<br />

{<br />

System.out.println(e);<br />

}<br />

private void out(String str)<br />

{<br />

printOut.println(str);<br />

System.out.println(str);<br />

}<br />

Save the docum<strong>en</strong>t to your hard disk as SimpleServer.java and compile it using a Java compiler, which creates a Java<br />

class file named SimpleServer.class.<br />

You can start the XMLSocket server by op<strong>en</strong>ing a command prompt and typing java SimpleServer. The<br />

SimpleServer.class file can be located anywhere on your local computer or network; it doesn’t need to be placed in the<br />

root directory of your web server.<br />

If you’re unable to start the server because the files are not located within the Java classpath, try starting the server<br />

with java -classpath . SimpleServer.<br />

To connect to the XMLSocket from your application, you need to create a new instance of the XMLSocket class, and<br />

call the XMLSocket.connect() method while passing a host name and port number, as follows:<br />

var xmlsock:XMLSocket = new XMLSocket();<br />

xmlsock.connect("127.0.0.1", 8080);<br />

Wh<strong>en</strong>ever you receive data from the server, the data ev<strong>en</strong>t (flash.ev<strong>en</strong>ts.DataEv<strong>en</strong>t.DATA) is dispatched:<br />

xmlsock.addEv<strong>en</strong>tList<strong>en</strong>er(DataEv<strong>en</strong>t.DATA, onData);<br />

private function onData(ev<strong>en</strong>t:DataEv<strong>en</strong>t):void<br />

{<br />

trace("[" + ev<strong>en</strong>t.type + "] " + ev<strong>en</strong>t.data);<br />

}<br />

To s<strong>en</strong>d data to the XMLSocket server, you use the XMLSocket.s<strong>en</strong>d() method and pass an XML object or string.<br />

Flash Player converts the supplied parameter to a String object and s<strong>en</strong>ds the cont<strong>en</strong>t to the XMLSocket server<br />

followed by a zero (0) byte:<br />

xmlsock.s<strong>en</strong>d(xmlFormattedData);<br />

The XMLSocket.s<strong>en</strong>d() method does not return a value that indicates whether the data was successfully transmitted.<br />

If an error occurred while trying to s<strong>en</strong>d data, an IOError error is thrown.<br />

Each message you s<strong>en</strong>d to the XML socket server must be terminated by a newline (\n) character.<br />

For more information, see XMLSocket.<br />

Last updated 6/6/2012<br />

804

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!