Flash Remoting ---- New Network App Development Mode
Development model of Flash Remoting
Since I started working in the background development of Java website three years ago, I have been looking for a relatively good development model. Throughout the many website development methods, although the development language is different, there is no significant change in form.
Whether you use Perl, PHP, ASP, JSP, the actual process is roughly like this.
1: Users to your site, enter information, click "Submit" button
2: The user waits for a response from the server, the browser loads the response page, and the user can view the results.
3: If the result set does not take more page, the user will click on the next page. Please submit it to the server again. The server's program is searched again to return the second page of the result page to the user's browser.
4: This is so recovered.
In general, it is the development method of Request / Response / Session. If you are used to developing desktop applications, you will not be used to this approach, two very critical technologies - component technology and event drive development are The website development model has been lacking for a long time. Traditional website development technologies involves three vocabulaions of Request, Response, and Session, whether it is, what technology is used, and cannot develop applications based on event drivers and components. Although Sun recently released JSF, try to add event-driven development methods to J2EE, but according to it, the effect is not good, now there are some good components developed, such as Apache's Tapestry, which is a way of using a component, will Request / Response / Session shields from the field of view in the developer, replaced by Object / Method / Property, developers can do not need to know the exquest / response / session existence, while developing their own components in an object, with Components to assemble their own web pages, its feature is the component development mode, MVC outstanding separation (I personally think that MVC is best), but it does not solve another very important issue - Event driver development . Event driver development is very common in desktop applications, people who have used VB, Delphi and other C / S development tools are unfamiliar. Why didn't the event driver in the B / S development mode. The reason is that the "stateless" feature of the HTTP protocol is used as a complete new user for each request from the browser. In order to maintain status information, many application servers provide "status management", which is usually said as SESSION management. . This gives the final user a seamless integrated illusion, as if each time you ask the server to identify the request from a specific user, the most famous example is the shopping frame of the shopping website. Based on this situation, it is very difficult to use event-driven development. There is also an HTML page itself is not suitable for event driver development. Each time no matter is large or small, it will re-brush the entire page. This development mode is also very efficient. Low, causing waste of a large amount of bandwidth resources, the server-side increase session management also increases the waste of server resources.
It is now truly employed by event-driven and component models to develop websites, probably only two: ASP. Net and flash recotiet. The former, I am not familiar, for server-side technology, it is too dependent on Microsoft, the client also relies on Microsoft, in general, it is a proprietary technology. Flash Remoting is Macromedia, front-end technology depends on the company's Flash, while the backend technology depends on Macromedia. Why do you say that Flash Remoting techniques consists of two parts, the front end part is used with flash flash transing components. Development, the backend uses Flash Remoting Gateway any of the technologies. Flash Remoting Gateway now has a lot of development source code implementation. This way Flash Remoting can basically say that there is many options in the server. So let us see what Rlash Remoting is something.
Simply put, Flash Remoting allows Flash directly to call the remote process of the server. The development of server-side does not require any adaptation to flash, which can be used to design a server program with any language and technology, using the best design mode, without using any flash API, and Flash Remoting, Flash Program Call any remote service. The most important technology for the Flash Remoting server is the Flash Remoting Gateway, which is installed on the application server, as an interface between the Flash player and the application server, it mainly completes three work.
1: Pass the request for the Flash player to the server
2: Translate requests and data from the Flash player into server-side requests and data
3: Translate the server-side response and data into the local data type of ActionScript.
The communication protocol used between the Flash player and Flash Gateway is a binary protocol AMF (Action Message Format) developed by Macromedia. It is a lightweight, efficient protocol, which can quickly serialize between them. Declined and transmitted data. In fact, if you are familiar with SOAP, you will find that this agreement is re-constructed in many cases, you can think that AMF is the SOAP protocol of Macromedia's binary version. Why is Macromedia to re-architecture? First of all: SOAP is based on XML, compared to binary AMF, it seems too cumbersome, or "heavyweight", second: AMF is mainly designed and optimized for the ActionScript type, it must be parsed and serialized More efficient than SOAP's general protocol format. Flash Player 7 can support SOAP, while the Flash 6 player can support SOAP via Flash Remoting Gateway.
Summary: Flash Remoting consists of two parts, Flash Remoting Components (Client) Flash Remoting Gateway (server side). Flash Remoting Components (clients) are mainly used to develop Flash Remoting program development, Flash Remoting Gateway (server-side) is used to translate translated from the Flash player to the server-side process, translating it as a request for application server identification transfer. The server then returns the result to the Flash Remoting Gateway, and then returns to the Flash player in the AMF format.
Some people may still don't understand now, Flash Remoting is not to let you call the service service directly, this is what the advantage is in the development method of Flash 5 XML. In fact, one of the most obvious is a bit of Flash Remoting, and the developers do not need to write the client and the server's XML resolution program. In addition, it can convert a large number of complex data types, automatically process data conversions of ActionScript and server. Using AMF to serialize data is more efficient than the serialization of this string for processing XML, of course, the consistent way to provide access to the background service is also a bit a bit, no matter what technology is used in the background, the way the front-end access is unchanged. of. It is also worth mentioning that flash recoting means transparent for users and servers, and session information is actually included in each AMF package without session management. Because Flash is loaded with a browser, the SESSION state is automatically kept on each time the server calls, the session information remains in Flash, so there is no difference between the server cluster.
Flash instance programming step by step.
Telling too many theories, it is better to use a specific example to make people understand what is Flash Remoting. Not in the beginning of a step, I want to explain the server environment that runs Flash Remoting. The Flash Remoting server can use a variety of technologies, and Macromedia is recommended is its own company's two products ColdFusion and JRun, and the bottom of ColdFusion is also based on JRUN, but it is more convenient to support Flash Remoting Access ColdFusion. Web and components. However, the price is relatively expensive (tens of thousands of yuan), and JRun is a SUN J2EE strictly certified application server, and the price is relatively cheap, only more than 700 dollars, so if it is the development method of Flash Remoting, I personally feel JRUN is More value. Of course, if you don't want to pay for the application server, there are still a lot of free open source solutions, such as the PHP project http://www.amfphp.org) and Perl item http://www.simonf.com/flap, for Java Open is OpenAmf http://www.openamf.org is a very good open source solution. If there is a chance, I will write a detailed writing of this open source project.
I am a Java programmer, the following example develops using Macromedia's JRUN server. About JRUN download and related knowledge, you can refer to the Macromedia website http://www.macromedia.com/software/jrun/?promoid=home_produ_jr_100803. a) Environmental construction
I hope that the new establishment of a JRUN service application is distinguished from JRun's existing admin, Samples, and Default three JRUN servers to create a new application through JRUN.
First start JRun Launcher,
Click Admin Jrun Server and click the "Start" button to start the server. Its port 8000, so we can use Jrun Management Console
Figure
Start JRun Management Console will open the following webpage.
Click on the top "CREATE New Server"
A new window for creating a JRUN server appears. Enter flashRemoting in "JRUN Server Name". JRUN will automatically update the contents of "JRun Server Directory" based on your input. Click "CREATE Server"
Waiting for a moment, JRUN will prompt to create success, and output information such as port (JRUN assignment) occupied by the new server. If you are not satisfied, you can modify it. As shown below
If you are satisfied, you can click the "Finish" button. If you are not satisfied with the port, you can modify the port number and click on "Update Port Numbers" to update.
Open the Explorer View, you will find that JRun has established a FlashRemoting directory for you in the Server directory, as shown
b) Development Services
First create a CLASSES directory under the server_inf directory of the flashremoting directory just established, as shown. JRUN supports Flash to call multiple J2EE technology, the simplest is Java Class, Java Beans, JMX, EJB I am here for Java Beans. Establish the following Java Bean file in the Classes directory that has just been established.
Package com.eiffelqiu.remoting;
Public class testbean imports java.io.serializable {
Private string testValue = "test";
/ **
* Returns the TestValue.
* @Return String
* /
Public string gettestValue () {
Return TestValue;
}
/ **
* Sets the testValue.
* @Param TestValue The TestValue to Set
* /
Public void settestValue (String TestValue) {
THIS.TESTVALUE = TestValue;
}
}
Familiar with Java will find that this is a very simple Java bean, even the constructor is saved, but the most critical part of this program is that I marked red characters, that is, Java Bean must implement serializable. If this interface is not implemented, we will not be able to take the value just set, if you want to keep status information, you must implement serialized interfaces. There are some examples of flash recitaling omitted this, and the results will result in some examples of flash call Java beans that cannot succeed.
Enter the CLASSES directory, compile this program in the command line: javac * .java -d.
Ok, all our server devices are completed. You may think that this is too simple. In fact, this official Flash Remoting is strong. The server program only wants to pay attention to their own technology. No need to know any technology about Flash, Flash Remoting Gateway is responsible for completing the Flash ActionScript object and the server. Translation and conversion work between objects. The server can be available as long as the interface is provided. This technique is very good, especially for large projects. According to the development mode of Macromedia Flash Remoting, it should be divided into three main characters, Flash Designer (responsible interface), Flash Programmer (client programmer, responsible for Flash Remoting client programmer), server programmer (you can use Technique). c) Develop client programs
The client's Flash program is not very troublesome. Establish a Client.FLA flash program.
As shown in the figure above, a total of six text boxes, two buttons
I list the naming rules separately.
The topmost block is the user filling in the input value, named SendText in the VAR variable name of the text box.
The button next to it is a MOVIECLIP, Instance Name: SetButton (click to pass the value of sendtext to the server's Java Bean)
The following big text box is used to return server information, named ResponseText in the VAR variable name of the text box
The button next to it is a MOVIECLIP, Instance Name: GetButton (click on the service segment information to the text box)
The lower two text boxes are set: gateway paths and class object paths
The variable name of the text box corresponding to the Gateway Path is GatewayPath
Two in the text box corresponding to Object Path is ObjectPath
I have set the default name here.
They are: http: // localhost: 8101 / FlashServices / Gateway and com.eiffelqiu.remoting.testbean
This is the Flash Gateway address I just established and the full path name of Javabean just created (called service name), you may notice that the port number I gave here is 8101, this is just we have established The port number of the new JRUN service is carefully observed, and I will find that I placed an Action layer for writing code. However, open this layer's script code, you will find that I only prevent a statement
#include "client.as"
Tip: Why do I do this, a lot of Flash programs do this. Exploring the main reason: it is to prevent someone to reverse engineering Flash to get the source file. Some people can get the FLA source file from the SWF file through some anti-compilers. So there are many flash developers to place their code outside, so when you deploy, you can place some webpage programs, such as JSP in the web-inflicment, so that users can access these addresses directly through the URL, and only programs can access To them, it achieves directory that prevents reverse engineering. This kind of thing is benevolent, just for your reference.
Now let's build a client.as file below the directory of the Flash file.
The document is as follows:
#include "netservices.as"
// ::: :::::::::::::::::::::::: :::::::::::::::::::::::
// ::: Button action Script
SetButton.label = "set value";
SetButton.clickHandler = function ()
{
NetServices.SetDefaultGatewayurl (_Root.gatewayPath);
ServiceConnection = NetServices.createGatewayConnection ();
ServiceObject = ServiceConnection.getService (_Root.ObjectPath, this);
ServiceObject.setTestValue (_Root.sendtext);
}
GetButton.label = "get value";
GetButton.clickHandler = function ()
{
NetServices.SetDefaultGatewayurl (_Root.gatewayPath);
ServiceConnection = NetServices.createGatewayConnection (); serviceObject = serviceConnection.getService (_root.ObjectPath, _root);
ServiceObject.gettestValue ();
}
// ::: :::::::::::::::::::::::: :::::::::::::::::::::::
// ::: :::::::::::::::::::::::: :::::::::::::::::::::::
// :: Define Data Handler
GetTestValue_Result = function (result) {
// Output Message HEADER
_Root.ResponseText = "server response:" Result
// trace ("Result:" Result);
}
// ::: :::::::::::::::::::::::: :::::::::::::::::::::::
The code is very simple, set the label name for two buttons, and establish a process function, Flash Remoting is fixed, the first is to set the default gateway address, which is set in the GatewayPath text box http: // localhost: 8101 / FlashServices / GATEWAY, then call NetServices.createGatewayConnection () to establish and connect to the server-side Flash Gateway. Use netservices.creategatewayConnection () call service, which is the Java Bean we created, and its address is passed through the text box variable ObjectPath. Then call GetTestValue
NetServices.SetDefaultGatewayurl (_Root.gatewayPath);
ServiceConnection = NetServices.createGatewayConnection ();
ServiceObject = ServiceConnection.getService (_Root.ObjectPath, _root);
ServiceObject.gettestValue ();
Flash Remoting Programming has a naming rule for default response functions. .
Now let's start our FlashREmoting server first.
Open JRUN LAUNCHER if you have already opened, click the "Refresh" button, you will see that the flashremoting that has just been established appears in the JRUN Server list. Select it, click on the "START" button
Run the Flash program, enter "Hello Flash Remoting" in the first text box, click the SetValue button, and then click the GetValue button. You will see the corresponding text box from the server:
"Server Response: Hello Flash Remoting"
This is an example of a relatively complete Flash Remoting, you may think that this example is too simple, Flash Remoting can make more complex programs, which does not have restrictions on the technology used by the server, but simplifies Flash and the server. Data between technology, the above example is only a point of traditional Flash Remoting technology, the new Flash Remoting for Flash MX 2004 ACIionscript 2.0 adds more things, many ways to write, I will in future articles Make an introduction.