AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=942&pId=-1
Working with Flash Remoting Using .NET
page
by Kr Hemendra Singh Shaktawat
Feedback
Average Rating: 
Views (Total / Last 10 Days): 36362/ 49

Introduction

Flash Remoting is a product from Macromedia Flash which works as an interface between the Flash application and your server side applications such as .NET, J2EE or ColdFusion.  The big advantage of the Flash Remoting is that you can pass live objects like XML objects to and from the server. The image below shows the overview of the Flash Remoting control flow.

Figure 1

Installation

Flash Remoting (evaluation version) is available at the Macromedia website (free to download) and the Flash Remoting component is also available at the Macromedia website for free.

There are two ways of using the Flash Remoting.  The first is to use it at your web server and the second is to use it with your Flash application development.  If you want to host an application which will use Remoting then you just have to install the Flash Remoting to your host machine. This requires that .NET framework already be installed on your host machine.

If you are developing a Flash application which will use Remoting then you have to install the Flash Remoting component on your machine.  To install the Flash Remoting component you have to have Flash MX or Flash MX 2004 installed on your machine.

You must restart your computer after you install the Flash Remoting or Flash Remoting component.  It will not ask you to restart the system, but it will not work until you restart the system.

Flashgateway.dll

Once you are done with the installation and are ready to start your first "Hello Remoting" application, you have to do a few small jobs to move forward.  While you installed the Flash Remoting it would have created a web application at "http://localhost/flashremoting."

The folder path is C:\Inetpub\wwwroot\flashremoting.  Go to this directory, open the bin and copy the flashgateway.dll and frconfig.txt to the bin directory of your .NET application.  The frconfig.txt has the licensing information in it.  We name the .NET application as MyRemoting and from now on we will refer the .NET application as MyRemoting

Gateway.aspx

Add a file named gateway.aspx to your MyRemoting directory.  This file will be empty and will not contain any code.  You can write one line as the following.

Listing 1

<%@ Page %>
<!-- This file is intentionally blank. -->

This file will be used by the Flash Remoting as the starting point to access your .NET application.

Web.config

Open the web.config file from MyRemoting directory and put the below mentioned code in the middle of the file.  This will be used by MyRemoting to refer Flash Remoting.

Listing 2

<httpModules>
<add name="GatewayController" 
type="FlashGateway.Controller.GatewayController,flashgateway" /> 
 </httpModules>

Register Flash Gateway with .NET

Add the two tags mentioned below to your .aspx or .vb file which will serve your Remoting call. For example, we will call it MyRemotingService.aspx from here onwards.  The first tag will register the Flashgateway with .NET and the other tag will be used if you put your server side code in the aspx file only.  You can add functionality to the code behind the file as well.

Listing 3

<%@ register tagprefix = "Macromedia" 
NameSpace ="FlashGateway" Assembly ="flashgateway" %>
<Macromedia:FLASH id="Flash" MACROMEDIA:FLASH MACROMEDIA:FLASH>
</Macromedia:FLASH>

We are done with the configuration, so what is next?

Now that we are done with all the setup for the Flash Remoting, we can start writing code for the Flash to communicate with our server application using Remoting.  We will create a Flash file which will communicate with the .NET Remoting server.

We will create a Flash application which has two sections.   One which takes user input for the user's first and last name and sends those to the server as the XML object server will concatenate the first name and last name and return the full name to the user as an XML object.  Another section will just take the user request and return server time in string format.

For this application at Flash end, we have one “MyRemoting.fla” and one external ActionScript file named “RemotingWithDotNet.as,” which contains the code for Flash to do Remoting.  At the server end we have a VB project, “MyRemoting.vbproj,” and “MyRemotingService.aspx.vb” to serve the Remoting calls as well as few other .NET created files.

We start with the Flash part first.  Create a Flash document and name it "MyRemoting.fla."

Figure 2

 

Here is the code that goes into the action layer of our .fla file.

Listing 4

#include "RemotingWithDotNet.as" 
_root.btnFullName.onRelease = function(){
var sXML:String = "";
 var oXML:XML;
if(_root.txtFirstName.text == "" || _root.txkLastName.text == ""){
_root.lblFullName.autoSize = "left"; 
 _root.lblFullName.text = "Please type your first name and last name."
 _root.status.text = "";
}else{ // Make XML string
sXML += "<root>";
 sXML += "<firstname>" + _root.txtFirstName.text + "</firstname>";
 sXML += "<lastname>" + _root.txkLastName.text + "</lastname>";
 sXML += "</root>";
// Create XML object
oXML = new XML(sXML);
 _root.status.text = "Connecting to the server...";
// Make a Remoting call to the server 
_global.oRemoting.ConcatenateName(oXML);
}
}
_root.btnServerTime.onRelease = function(){
_root.status.text = "Connecting to the server...";
// Make a Remoting call to the server 
_global.oRemoting.GetServerDateTime();
}

The #include "RemotingWithDotaNet.as" does the inclusion of our external file.

The other two functions are called on the different button click.  The root.btnFullName.onRelease() picks values from the input boxes, prepares the XML object and makes a request to the Remoting server.

The function _root.btnServerTime.onRelease() does not need any XML to be sent, so it makes the server call as soon as the button is clicked.

Here is the code from our RemotingWithDotNet.as file.

Listing 5

// This will be used for getting Remoting services.
#include "NetServices.as" 
_global.oRemoting; // Global Remoting object, will be used to call Remoting services.
function init() 
{
       if (this.inited != undefined) 
       { // init is not required.
       return;
} else { 
       
       this.inited = true;
       NetServices.setDefaultGatewayUrl("http://localhost/myremoting/gateway.aspx");
       gatewayConnnection = NetServices.createGatewayConnection();
//This is our default relay interface     
defaultService = "myremoting.MyRemotingService";
       _global.oRemoting = gatewayConnnection.getService(defaultService, this);
}
}
// RESULT FUNCTION FOR: oRemoting.ConcatenateName()
function ConcatenateName_Result(result) 
 {
       _root.status.text= "";
       result.ignoreWhite = true;
       trace("result :: " + result);
       _root.lblFullName.autoSize = "left"; 
       _root.lblFullName.text = "Hello " + result.firstChild.firstChild.toString(); 
 }
function ConcatenateName_Status(error) 
 {
       _root.status.text= "ERROR: " + error.description + newline;
 }
// RESULT FUNCTION FOR: oRemoting.GetServerDateTime()
function GetServerDateTime_Result(result) 
 {
       _root.status.text= "";
       result.ignoreWhite = true;
       _root.lblServerTime.autoSize = "left";
       trace("result :: " + result);
       _root.lblServerTime.text = result.toString();
 }
 
 
function GetServerDateTime_status(error) 
 {
       _root.status.text= "ERROR: " + error.description + newline;
 }
init();
 stop();

The first line of the code #include "NetServices.as" includes the file NetServices.as that will provide us the implementation of Remoting functionality.  The init() function instantiates the Remoting object.  In our case that is "_global.oRemoting."  We are done with the Flash side code; here is the server side code which will serve our Remoting requests.

Listing 6

Imports System.Web.UI
 Imports System.Xml
Public Class MyRemotingService
 Inherits System.Web.UI.Page
'--- This function accepts a XML object with two child nodes as first name and
 '--- last name and returns the full name in one node.
Public Function ConcatenateName(ByVal oXMLIn As XmlDocument) As 
 XmlDocument
Dim oXMLOut As XmlDocumen
 oXMLOut = New XmlDocument
 oXMLOut.InnerXml = "<root>" & _
 oXMLIn.FirstChild.ChildNodes(0).InnerText & " " & _
 oXMLIn.FirstChild.ChildNodes(1).InnerText & _
 "</root>"
 Return oXMLOut ' return the new XML object.
End Function
Public Function GetServerDateTime() As String
Dim sServerDate As String
       sServerDate = Now().ToLongDateString.ToString & " " & Now().ToLongTimeString.ToString
       Return sServerDate
End Function
End Class

This class has two functions: ConcatenateName() and GetServerDateTime().  We can call these from our Flash application.

How application works

When we make a request to the Remoting server from Flash it processes our request and returns the result back to Flash.  Flash Remoting uses call back functions to handle the server reply. These functions, as present in the code above, use the same function name we call to process our request with suffixes as _Result for the Result and _Status to handle any error if it occurred at the server while processing our request.

So when everything goes well and the server returns as expected, the call goes to _Result call back function and if it does not, the call goes to the _Status callback.

Benefits of working with Remoting

This gives us the ability to connect to and read live data, XML, and consume Web services or business logic at application servers such as .NET, J2EE or ColdFusion.

Downloads
Questions

Can I change the "Gateway.aspx" to another file as this file is a blank file?

Can I access multiple Remoting services from one Flash application?

Can I keep Remoting service responses (*_Result and *_Status) to another file?

Can I rename "xyz_Result" and "xyz_Status" to "xyz_somethingelse?"

If you know the answers to these questions, congratulations you know the concept of Flash Remoting!  You can write your answers or queries to me and I will get back to you.

Summary

So, the Flash Remoting gives us such benefits as:

·         The flexibility to connect to any middle tier application developed in .NET, Java, ColdFusion and PHP.

·         Complete UI logic can be shifted to client side as the server has fewer loads and performs better.

·         Separates the UI from middle tier and the backend, so all can be independent.  If have your Remoting middle tier in .NET today and tomorrow you want to switch to Coldfusion, you can do it without touching the UI at all.

In short, the Flash Remoting gives us the beauty of rich Internet UI and the power of web application with a great user experience.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-23 3:42:24 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search