Saturday, September 1, 2007

Lindsey Dawn Mc Kensey

Introduction to Flash Remoting AMFPHP

times are changing, changing versions of Flash, but most users do not change their habits, still using the methods which will soon be deprecated, or require a lot of the west for larger projects.



Technology Flash Remotingu is available from 6 versions of Flash. However, due to low availability for the average developer and a lack of materials on this flashed quietly not paying attention. This technology has its implementations. NET, Java, ColdFusion and other languages \u200b\u200bserverside built by default, or as separate library, low percentage of users of these technologies led to the remoting does not become too popular. However, some time is available for free (which is important) implementation in PHP



And here our story begins.



What is remoting?



Remoting is a simple way to exchange data between Flash and server language. What is different from LoadVars, etc.? There is a lot of arguments.
when transferring to a Flash one, two variables is not a problem. When you want to send a larger package of data using xml. And what if you want to use the database?
calling the server side sql query, translates it to XML (or even a string) you upload to Flash where you change into the back of a string of variables, objects, arrays, etc.

For such a simple thing is a bit of a long road. A significant loss of time and resources on a simple exchange of information.



Flash Remoting is a toolkit which includes a gateway server, a set of components, classes, and debugger. On the side of a set of classes you have Flash to encode and decode data. Similarly, a server-side gateway that performs the same task, and calls the desired method. Data is exchanged in binary format inspired by the AMF SOAP. Both gate and flash the class translate the data on neutral format, and it allows for the exchange of data between the various technologies behind the scenes.




What boils down to one thing - the exchange of data between the flash and in our case PHP is fast and simple.





So what advantages remotingu decide to abandon completely the other methods?

three basic factors should persuade you to do so - the behavior of variable types (String, Number, Array, Object [hash associative array if you like]), real objects returned by the sql query (there are no fetch_array etc.) and mapping of the classes ( What we develop here is not, unfortunately)



After all the raptures of technology, let's get down to particulars


Requirements AMFPHP

For correct operation is required PHP 4.3 and higher to 5 inclusive. It's official requirements, but in our situation can be said about needed for iconv library version 4.3 is available here to the correct handling of Polish characters. However, it is usually available on the servers, and PHP 5 is installed by default.

update to JanekCanI
You do not need to have installed any libraries that converts characters between other systems, if you have access to php.ini, and you can change the default encoding php pages. Then simply
$ gateway-> setCharsetHandler ("none", "utf-8", "utf-8");



Installation



installation pack downloaded the latest version of AMFPHP here or directly from the project download section.

Update:
AMFPHP 1.9 beta version can be downloaded here
But remember that this version differs from that described in the tutorial, no methodTable min and deactivated several typical ficzersów.




side

PHP server-side installation is reduced to copying the contents of the installation directory on the server. Ensure the proper functioning of the settings in the file gateway.php. The main change that we must introduce is to find this line




/ / $ gateway-> setCharsetHandler ("utf8_decode", "ISO-8859-1", "ISO-8859-1");



and replace the


$ gateway-> setCharsetHandler ("iconv", "ISO-8859-2", "ISO-8859-2");





What will enable us to support Polish characters. If this setting it does not work for Polish characters, you can try other versions, or replace this line at




$ gateway-> setCharsetHandler ("iconv", "UTF-8" "UTF-8");







If you feel that everything was done as it should, and later calling the methods of flah it does not work (even though in the debugger you can see that it was successful) check that you have iconv library installed, in the absence thereof, and such a setting in gateway.php AMFPHP does not need to return an error.
After preparing everything, run the file in your browser gateway.php possibly in search of errors. If your browser tries to download a file, it means that everything went as expected. Remember to leave all the files on the server, they are required for the proper functioning of the Federation. Only the Service Browser is advisable not to place on the final server, because it contains a preview of all the methods.

Remember that one installation supports an unlimited AMF (never given a limit) the number of services, including one plant can be used in parallel to many projects.





File debuggateway.php used to test and return a greater number of errors, but remember that when you start the system to switch system on gateway.php, because it is much more efficient.





side flash

remotingu access to classes and get after installing the extension that can be downloaded here



After installing this extension you get 5 basic elements



  • NetConnection debugger, the movie flash, which shows the course of inquiries in both directions. You can find C: \\ Documents and Settings \\ {username} \\ Local Settings \\ Application Data \\ Flash {in} \\ {language} \\ Configuration \\ WindowSWF \\ NetConnection Debugger.swf in flash or Window> Other Panels> NetConnection Debugger

  • Service Browser, which helps to view your services (like on-line version built into AMFPHP)

  • Help files, slightly protruding from the conventions of other parties to help, but they are a major source of information.

  • remotingu class libraries available in flash under Window> Common Libraries> Remoting or Window> Others> Libraries> Remoting

  • available
  • Component Connector Components> Data Components> RemotingConnector.




sure to include both packages (RemotingClasses and RemotingDebugClasses) in the library of every movie that uses FR






Testing


When all elements of time to test the system.

so the first will create a service (Service) server-side. Create a file HelloWorld.php and type in the


 


class HelloWorld {



function HelloWorld () {



$ this-> methodTable = array

(

"Scream" => array

(

"access" => "remote",

"description" => "Returns the sent message"

)

);}





function yell ($ message) {



return 'Dre' button. $ Message;



}}

?>


Thus prepared, place the php file in the folder of your installation services in AMFPHP

In this folder you must place all prepared for its services. You can view them by going in the folder browser, which is placed a simple and functional web browser of your services, together with the generated AS code to use.





Now in Flash, create a new document, go to Window> Common Libraries> Remoting
(or Window> Others> Libraries> Remoting depending on the version of flash) and drag the two parcels on the scene. Of course, even though they are visible they are not their graphical representations after the publication of the film.

Then in the first frame of the movie, paste the following code


 

mx.remoting import .*;

mx.rpc import .*;

import mx.remoting.debug.NetDebug;



gatewayUrl var: String = "http://localhost/flashservices/gateway.php" / / here, of course, the path to your installation AMF file instead gateway.php better when testing debuggateway.php use file, which is located in the same folder, and send more information to NetDebugerra



NetDebug.initialize ();

var _service: Service = new Service (gatewayUrl, null, 'HelloWorld', null, null) / / arguments are accepted: [path to goal, logger, the name of a method call, the responder]

/ / logger

/ / object in which the logs will be created, more information in the search for Log class

/ / call

/ / allows you to use an existing connection, such as nazwaUslugi.connection,

/

responder / responder object containing methods to support the results and errors



var pc: PendingCall = _service.krzycz ("Hello world !");// call a method and passing it some arguments, remember that you can communicate not only strings but virtually all of the available types of Tablets, including



pc. responder = new RelayResponder (this, "OnRezultat", "onError ");//

/ responder object is forwarded to it by calling the methods / functions after receiving a response from the server

/ / first argument is an object that receives the response, usually corresponds to the time line which includes the functions that support the results

/ / If this path is incorrect script does not react to the response from the server, which is a common mistake in the beginning.



onRezultat function (re: ResultEvent) {/ / This function is passed an object with the result returned by the method, eg the result of sql query, but more on that later

trace ('What drzesz,' + re.result);}





function onError (fe: FaultEvent) {/ / here with railways shall be forwarded to the error object

trace ('An error occurred, with the following Content: '+ fe.fault.faultstring) / / and so you can get to the content

}



Test the movie and displays a look at your Output window. You should get "What drzesz, Dre in Hello world!"




If, however, resulted in a prompt messages:


If you have information ** Error ** Scene = Scene 1, layer = Layer 1, frame = 1: Line 8: The class 'Service' could not be loaded. (...) You have not added correctly
classes for the film Choose Window> Common Libraries> Remoting (or Windows> Others> Libraries> Remoting) and drag the two classes on the scene, and later you can delete them, were important to the library of the film.



Error opening URL "http://localhost/flashservices/gateway.php"
incorrectly entered the path to the goal, or the server is not running.



"An error, which reads as follows: (...)"
Check the contents of error or look in the NetConnection Debugger window. You've made an error in the PHP class, or you do not type method to methodTable



If you do not receive any message, and the film after sending a query (which is seen in the debugger) does not correspond, check, written in PHP classes (by calling it in your browser), or check whether the firewall is not blocking requests.


types of data to be transferred



As mentioned several times before remoting allows to conduct the types of variables sent to both parties. The following table and are served with various types of restrictions.


























































array Array

types supported
Type in a flash `u type in PHP comments Auto Service
null null
so
Boolean (boolean) boolean
so
String string
so
Date float Unix timestamp, set the return type of the variable on the Date not

so
Object associative array
so
XML String Set return type to the XML variable to send to AS not
Recordset Resource Only from PHP to AS, not vice versa as


For types with automatic operation is not necessary to declare the return type. In another case
you need to apply a solution similar to that here


 


class RemoteData

{

RemoteData function () {



$ this-> methodTable = array (

"getRemoteDate" => array (

"description" => "Returns the server time as a Date object to flash",

"access" => "remote",

"returns" => "Date"

), / / \u200b\u200breturns determines the type of the return variable

"getRemoteDateString" => array (

'description' => 'Returns the time on the server in the form string in the format "Sun Mar 12 0:06:30 GMT 0800 2006 '',

"Access" => "remote",

)

"getTimezoneOffset" => array (

"description" => "Returns the difference in minutes, between universal time and the time server",

"access" => "remote",

)

);}





getRemoteDate function () {



/ / Note: you must return the time in milliseconds multiplied by the * 1000

return time () * 1000;

}



getRemoteDateString function () {



return date ('DM d H: i: s \\ G \\ M \\ IT Y', time ());}





getTimezoneOffset function () {



return date ('Z') / 60;



}}

?>




I hope that you are brought closer to the dimension of flash data exchange serverside, and encourage them to bid farewell to LoadCośtam.

Happy testing.

0 comments:

Post a Comment