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.
In order to make the most of this article, you need the following software and files:
flashlite_sockets.zip (ZIP, 14K)
You should be familiar with with Flash and Java programming.
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.
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.
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:
<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>
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.
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);
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.
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.
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.