Accessibility
Felipe Andrade

Felipe Andrade

i2tecnologia.com.br
felipeandrade.org/blog/
flashlite.com.br

Created:
6 August 2007
User Level:
Intermediate, Advanced
Products:
Flash Lite

Developing multiplayer applications with Flash Lite XMLSockets and Java NIO

This article takes you through XML Sockets connections in Flash Lite 2.1 and provides a hands-on demonstration of how to create mobile Flash Lite multiplayer content with a Java NIO Server.

Requirements

In order to make the most of this article, you need the following software and files:

Flash CS3 Professional

Sample files:

flashlite_sockets.zip (ZIP, 14K)

Prerequisite knowledge

You should be familiar with with Flash and Java programming.

No timers, no polling…just real-time

A socket is the communication mechanism that makes a connection among computers through programs constant. Once a socket connection is established, the client machine can access a set of services on the server machine. Sockets are used as endpoints for sending and receiving data between computers.

XML Sockets in Flash Lite are sockets that you can use to implement a full-duplex stream. No timers, no polling...just real-time! An XMLSocket object is useful for client-server applications that require low latency. Flash Lite implements client sockets that allow the device or computer running Flash Player to communicate with a server computer identified by an IP address or domain name and a port.

Sockets can be used as a multiplayer application that lets you interact with others users in real time and should maintain an opened connection to the server, allowing the server to immediately send any message to connected clients. A socket example can include real-time data transfer for information, such as chats and stock numbers.

In this tutorial, we will do a Flash Lite real-time chat using the Tornado Java NIO Server designed for Flash Lite applications. Tornado is an Open Source project hosted on Source Forge and developed by i2tecnologia and talented Java developers around the world.

Using the XMLSocket object

The XMLSocket object lets you establish a communication channel with a socket server application. To use the XMLSocket object, you will need a server computer to run a daemon that understands the protocol used by the XMLSocket object. In this tutorial, you are using an open socket server in Java, but instead of Java you can make it in Python (like in Flyer), Ruby, Perl, and the like.

All messages are based on XML as a data format for messages in both directions. An XML message is a complete XML document, terminated by a zero byte '\0' as the EOL (end-of-line) character. An unlimited number of XML messages can be sent and received over a single and persistent XMLSocket() connection. The XML messages are sent over a full-duplex TCP/IP stream socket connection.

Security on XMLSocket connections

As we saw, the XMLSocket object establishes and maintains an open connection to the server. For security reasons, the following restrictions have been placed on the XMLSocket object:

  • The XMLSocket.connect() method can connect only to TCP port numbers greater than or equal to 1024, because ports smaller than 1024 are sometimes referred to as "well-known" ports, and are often used by system services such HTTP (port 80), FTP (port 21), POP (port 110), SMTP (port 25), and TELNET (port 23). One consequence of this restriction is that the server daemons that communicate with the XMLSocket object must also be assigned to port numbers greater than or equal to 1024. The port number restriction limits the possibility of these resources being inappropriately accessed and abused.
  • SWF files loaded from a domain server into the Flash Lite player may only connect to services available from ports in the same domain, unless a cross-domain policy file (crossdomain.xml) exists in the web root directory of the server. Given below is the data structure of a xml file:
<cross-domain-policy>
<allow-access-from domain="*"/>
         <allow-access-from domain="*.i2tecnologia.com.br" secure="false"/>
         <allow-access-from domain="*.adobe.com" secure="false"/>
</cross-domain-policy>

Methods and Properties

Method summary for the XMLSocket object:

XMLSocket.close: Closes an open socket connection.

XMLSocket.connect: Establishes a connection to the specified server.

XMLSocket.send: Sends an XML object to the server.

Event handler summary for the XMLSocket object:

XMLSocket.onClose: A callback function that is invoked when an XMLSocket connection is closed.

XMLSocket.onConnect: A callback function that is invoked when an XMLSocket connection is established.

XMLSocket.onData: A callback function that is invoked when an XML message has been downloaded from the server.

XMLSocket.onXML: A callback function that is invoked when an XML object arrives from the server.

Example

A real-time chat in Flash Lite is a simple task for beginners in ActionScript. Using the XMLSocket class, you can easily make the code very powerful.

// Randomizes the user nick name
var nickName_str:String = "a" + Math.floor(Math.random() * (999 - 1)) + 1;
// Creates the XMLSocket object that will handle all transactions
var socket_xmls:XMLSocket = new XMLSocket();
// Sends a message for various connected clients
function sendMessage(msg_str:String):Void {
      socket_xmls.send(nickName_str + ": " + msg_str);
      message_txt.text = "";
}
// Invoked when the XMLSocket connection is established with the server
socket_xmls.onConnect = function(success_bool:Boolean):Void {
   if (success_bool) {
      board_txt.text += "Welcome " + nickName_str + "!" + "\n";
      sendMessage("Hello World!"); // Send an intro message 
   } else {
       board_txt.text = "Connection failed" + "\n";
   }
}
// Invoked when an XMLSocket connection is closed
socket_xmls.onClose = function() {
      board_txt.htmlText += "Server closed." + "\n";
}
// Invoked when an XML message has been downloaded from the server
socket_xmls.onData = function (value_str:String) {   
       board_txt.htmlText += value_str + "\n";
}
// Establishes a connection to the localhost - port 3000
socket_xmls.connect("localhost", 3000);

Conclusion

When working in Flash Lite multiplayer applications, keep in mind that mobile data has a price. You need to load only the data that is needed. Always make sure that you have control over the data sent to your application.

The advantage of Flash Lite over other technologies is that Flash Lite can focus on the presentation of the data, and not necessarily the manipulation of it. Flash front-end can be combined with other languages through the XMLSocket object to provide a high-scalable application.

Where to go from here

I've been busy at work and I didn't have enough time to do all that I wanted to in the Tornado Java NIO server and sample files. I would like to see any improvements and additions you would like to add to the Tornado server. Contact me at my blog, felipeandrade.org/blog/ .I'm proud to learn from people around the world and also contribute my two cents of knowledge.

About the author

Felipe Andrade is CTO and cofounder of i2 tecnologia, a web and interactive company based in Brazil. He is the original creator of Flyer Framework and shares 60% of all donations to Brazilian children. Felipe has expertise in Flash development and has been working with ActionScript for six years. He's an instructor of Flash, ColdFusion, Dreamweaver, and Fireworks; and is experienced with Java and JavaMe. Felipe is the project leader for all games and applications developed by i2 tecnologia, including the award-winning Bone Smashers Arm Wrestling for the Flash Lite platform. He is manager of BRMAD – Adobe Mobile and Devices User Group Brazil and enjoys surfing on weekends.