Reputation: 19
How many possible ways to send/load data to/from server side data sources and what are the conditions of using each method.
Upvotes: 0
Views: 1547
Reputation: 5978
For simple communication, there are three common ways:
LoadVar
(AS2) / URLLoader
(AS3)It's historically the first method used by Flash to retrieve data with HTTP from a server.
ex:
//params.txt is a local file that includes: firstName=Tom&lastName=Jones
var lbl:TextField = new TextField();
var urlRequest:URLRequest = new URLRequest("params.txt");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);
function urlLoader_complete(evt:Event):void {
lbl.text = urlLoader.data.lastName + "," + urlLoader.data.firstName;
addChild(lbl);
}
It's quite fast to read from the Flash, but it's not so simple to do server-side as the syntax is really specific to Flash. And it becomes really bad when dealing with complex objects.
If you have to send some data to the server you can either put them as GET params in the URL or use URLVariables which is quite similar to URLLoader.data
.
Easy to read by a human, many libraries exist server-side. In AS3, the E4X syntax made it waaay easier to parse than in former AS2. Anyway it's useful for hand-typed data, not so easy to use with dynamic data. The main con remains
Well it is a tremendous waste of time; a lot of resources are wasted figuring out what schema to use, implementing code on server and client, and debugging. All of this for the purpose of constructing something on the server that you immediately destruct on the client side.
This quote comes from AMFPHP which is the... third way!
It is the best way (among simple ways) to communicate with a server through Flash. The communication is made with webservices. Flash-side the implementation exists natively (check out NetConnection). Server-side, AMF[whatever language you're using server-side] will allow you to use your typed object and send them directly to flash without any manual conversion. You can also send some data from the Flash without transforming it and you will find a typed object on the server. This is quite ideal.
To sum it up: URLLoader is good if you have let's say 3 variables (for very small projects). XML is perfect when you have quite a large amount of static data (a configuration file). AMF is ideal when you deal with a server that injects dynamic data (from a database).
Upvotes: 2
Reputation: 1937
HTTP: This is what URLLoader gets you. You can make HTTP requests of a webserver, and get back the results. The request can be as simple as fetching a text file, to as complex as calling a server-side script (PHP, ASP, Python,etc..) and getting back anything you can send by HTTP.
XMLSocket: You can use the XMLSocket class to make a direct TCP/IP connection to a server on the port of your choice and talk XML directly over the persistent connection.
Socket: The socket class works the same as XMLSocket, but for raw binary data over TCP/IP. The Socket class gives you a number of helper function for interpreting the data as common data types, as well as the capability of getting the raw data as a ByteArray. You can send anything you want this way.
What Flash does not have support for at this time (to the best of my knowledge) is complete built-in support for UDP. This is normally employed for low-latency P2P gaming, and I hear it's in the works and partly supported in the Flash Player 10 beta, but I haven't looked into it.
Upvotes: 0