1. Introduction
Macromedia's Flash is now widely used on the Internet to enhance the content description of Web sites. Macromedia's recent releases of Flash (Flash 6 and Flash MX) have made it clear that Macromedia is seriously considering Flash as an interface of choice for Web application development. For an interface, Flash may be a great choice; however, this choice is not to use it to replace business logic, query management and security. For a Web developer, to achieve truly powerful performance and enterprise-class capabilities, you need .NET, Flash and rich description layer support, combined with Microsoft's .NET technology.
Flash includes ActionScript - a scripting language used to add interactivity to animations. Since the introduction of ActionScript in Flash 5, Macromedia has continued to improve it from a simple scripting language to a fairly solid programming feature that still has many limitations. In Flash MX 2004, Macromedia introduced ActionScript 2.0 - which supports new features and new ways to structure code. To make this article easier to understand, you should first read some basic knowledge about building Flash sites with ActionScript.
Macromedia has introduced methods to easily integrate external data into Flash applications to allow for more complex and robust data-driven applications. Flash and .NET both promote themselves as cross-platform solutions and thus become natural partners. Still, linking the two technologies is no simple task. This article will discuss with you several methods of integrating Flash and .NET applications.
2. Integrate Flash and ASP.NET
and embed Flash animations into ASP.NET pages.
Macromedia Flash animations are placed in .aspx pages by jointly using the OBJECT and EMBED tags. The EMBED tag was originally developed by Netscape to work with Netscape 2.0 or higher. Later, this tag was adopted by Microsoft Internet Explorer (MSIE). For browser compatibility, the MSIE-specific OBJECT tag and EMBED tag can be used together. The following HTML code shows the use of OBJECT tags and EMBED tags to embed a Flash animation (*.swf file):
3. Connect Flash to external data.
There are three ways to integrate Flash animations with external data: FlashVars, LoadVars and the new FlashRemoting technology. One of the simplest methods is to pass external data from an .aspx page to a Flash animation embedded in a Web page by using FlashVars to pass variables in the HTML portion of the code. The values of these variables set using FlashVars can be accessed at the root level of the Flash animation. To run in all browser environments, the FlashVars attribute must be assigned a value in the OBJECT and EMBED tags.
Here is the FlashVars used with an object tag:
<PARAM Name="FlashVars" Value="init=yes&check=true">
The following is the FlashVars used with the EMBED tag:
<EMBED src="test.swf" FlashVars="init =yes&check=true"></EMBED>
4. Why use FlashVars?
Instead of borrowing FlashVars, you can also put variables in HTML tags - add them to the end of the URL of the .SWF file through a query string, and then pass it to a Flash animation. There may be a problem with this approach - if this variable is required when the .SWF file is loaded, since a .SWF file should be downloaded and run before the data is requested from the server. Variable values may also be set in Flash via the "Load Variables" method and JavaScript. Each of these methods has the disadvantage of laggy and slow response times; this is why developers should use FlashVars to pass data from ASP.NET to Flash animations. The following figure shows the code for passing variables as a query string:
Any variables passed using FlashVars can be accessed at the root level of the Flash animation (_level0). To retrieve these values within a Flash animation, you can create a dynamic variable. The following ActionScript code implements the assignment of "initvalue" in Flash:
_level0.initvalue = init;
5. Communication between Flash and database
A Flash animation cannot directly access a database. However, Flash can communicate with the database via any server-side technology like ASP or ASP.NET. There are several ways to accomplish this task. In this article, we will explain the LoadVariables method and sendAndLoad method.
(1) Use the loadVariables method in the animation clip.
The loadVariables method of the animation clip reads data from an external file or URL and sets the value for the variable of the animation clip. Once the animation clip is instantiated, the animation clip's loading action is initiated in the clip event event handler. When the animation clip is initialized, the loadVariables method can be called to load variables. The loadVariables method is an asynchronous call and Flash does not wait for the result of the call. When data is received in a loadVariables() behavior, the animation clip's data behavior is initialized in the clip event event handler.
To demonstrate the use of this method, this example retrieves the user's name and age from a database based on an ID selected from a Flash animation. In the figure below, the animation's loadVariables method is used to load data into the animation fragment from an .aspx page getUserData.aspx. Here, ActionScript passes the user's id - his name and age will be displayed in the Flash animation as a query string to the .aspx page.
The getUserData.aspx page retrieves the id value from the query string shown below:
Now, ASP.NET can pass this value id to .NET business components. This example retrieves the user's name and age:
As mentioned earlier in this section, the data behavior of the animation clip's onClipEvent event handler is initialized when data is received in a loadVariables() behavior. Declare the following dynamic variables in the Flash animation: flashName and flashAge. The following figure shows the ActionScript code that is used in the Flash animation's onClipEvent handler to retrieve the value returned from the .aspx page.
NOTE: It is important - it is the same variable that is set from the .aspx page. In the example shown here, the variable accessed in the Flash animation is "name" - the same variable it sets from the .aspx page (Response.Write("name="&User.name)).
These values can now be used inside Flash animations.
(2) Use the sendAndLoad method from animation clips
On today's Internet, XML occupies an important place in the form of a document system as a universal transport format with the potential to allow smooth exchange of data even between different types of applications. XML provides a gateway mechanism for communication between applications - even applications on a wide range of different systems. As long as applications can share data (via HTTP, file sharing, or another mechanism) and have an XML parser, they can share structured information that is very easy to process.
Flash MX supports XML data transfer and there are several server technology methods like ASP and ASP.NET for exchanging XML. Flash MX is able to send and receive XML packets by using the XML classes - load, send and sendAndLoad or Flash MX's new XMLConnector. The last one can be used to send and receive packets over HTTP and bind them to UI widgets in the Flash layer.
Let's discuss the sendAndLoad method of the FlashXML object, which is a popular method for implementing architecture-oriented XML data communication, exception handling and performance. We use this simple example of inserting a user's score details into a database from a Flash client and sending a success or failure message back to the Flash client.
(3) Create an XML packet in Flash
An XML packet can be easily created in Flash, as follows:
var sendXML:XML = new XML("<userid>123456</userid>");
Send an XML packet To a server side page - it uses ASP/ASP.NET and handles the return XML packet.
The usage is as follows:
xmlObject.sendAndLoad (URL: String, targetXMLObject: XML): Void
This 'xmlObject' is an XML object instance - it encapsulates the XML that must be sent to the .aspx page; this 'URL' is the destination URL that specifies the XML object; 'targetXMLObject' is the XML object - it will receive the information returned from the server side.
The sendAndLoad method sends an XML packet to a server-side template and receives an XML packet in response - unlike the send method of the XML class - which only sends an XML packet and does not expect any kind of response. Each of these methods works in its own way, but there is one important difference: the send method of the XML class takes a target as a parameter - it allows you to open a new browser window or replace the contents of the current browser window, while The sendAndLoad method takes an XML packet as a parameter - it does not start a new browser window. Because the send method opens a new browser window, we can see the output of the browser window itself - which can be extremely useful for debugging.
Note: If the Flash file issuing the sendAndLoad call is running in a Web browser, the target URL will be in the same domain as the Flash animation. If you also need to communicate with applications in different domains, refer to "Cross Domain Data Loading" at
www.macromedia.com for more information.
The code in the following figure shows how an XML object's sendAndLoad method is used to communicate with an .aspx page and process the XML returned from the server:
The above code does the following:
·Loads the XML string into an XML object "userscore_xml". The instance holds the XML - which is sent to the remote server side ASP.NET page.
·Set the ignoreWhite attribute of the XML object "ResponseXMLObj_xml". This XML object instance holds the XML packet from the server.
·Call the ("userscore_xml") sendAndLoad method of the XML object to send XML to the server-side .aspx page,
http://localhost/MyTestApp/scoring.aspx and store the returned XML package into the XML object instance "ResponseXMLObj_xml".
·Set the onLoad method of the XML object "ResponseXMLObj_xml". Flash receives the XML response back from the server-side .aspx page - it is handled in the XML object's onLoad method.
·It then parses the received XML packet to display the necessary messages on the Flash client.
(4) Receive the XML package sent from the Flash of the ASPX page.
The following figure shows the code to complete this task:
The above code does the following:
· Receives the XML sent from the Flash client (xmlDoc.Load(Request.InputStream)).
· Parses the received XML and passes it to the business component for insertion into the database.
· Formed XML message is sent back to Flash client. Success or exception conditions raised at the business layer are handled accordingly and converted into various XML messages - this message is passed back to the Flash client. The Flash client receives these XML messages and displays them accordingly.
6. Conclusion
Flash and .NET technologies are gaining popularity in the market today and are likely to become successful partners together in an increasing number of application architectures. These two techniques deal with some of the same problems, but are fundamentally different. Flash MX enables Web sites to experience interactivity, while .NET helps build enterprise applications. As explored in this article, integrating Flash and .NET applications is entirely possible. Every step is about making a decision - how to connect these two parts and do the right architectural planning strategically.
Although a new generation of architectures around Flash Remoting is gaining momentum in this area, it will be several years before the methods discussed in this article can be applied to large-scale applications.