Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Article

Flash and .NET with FlashRemoting

Rate me:
Please Sign up or sign in to vote.
2.27/5 (9 votes)
1 Jul 20052 min read 100.7K   1.7K   45   14
Testing FlashRemoting with .NET.

Introduction

This article explains how FlashRemoting works both in .NET and Flash. We use FlashRemoting for fast and secure communication with the database or other stored information and display it in a Flash movie. In this example we don't use any database, this is the simplest way to test it.

Using the code

First you have to install FlashRemoting from Macromedia and you have to have Macromedia Flash MX 2004 and ActionScript 2.0 installed.

Now we can start with the .NET code. First you have to start a new web application project in Visual Studio, and add the FlashRemoting server control as a new reference. You will find it in c:\inetpub\wwwroot\flashremoting\bin\flashgateway.dll.

Then you have to add this code in your Web.config under the system.web tag:

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

Now we can start "coding". You have to make an inited page for the Flash so that Flash can connect to the .NET application. Just create a new page called "gateway.aspx".

To get the parameters in .NET application from Flash and then for sending the data to Flash, you have to add a server control into the page, so just make sure that your ASPX file looks like what is given below:

ASP.NET
<%@ Register TagPrefix="Macromedia" 
    Namespace="FlashGateway" Assembly="flashgateway" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" 
    AutoEventWireup="false" 
    Inherits="FlashRemotingTest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta name="GENERATOR" 
          Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" 
          content="JavaScript">
        <meta name="vs_targetSchema" 
          content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <MACROMEDIA:FLASH id="Flash1" Runat="Server" />
        </form>
    </body>
</HTML>

And now the Codebehind. For sending information to Flash from a .NET application you have use the server control named "flash1". In this sample, we will load a simple string in Flash:

C#
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace FlashRemotingTest
{
    /// <SUMMARY>
    /// Summary description for WebForm1.
    /// </SUMMARY>
    public class WebForm1 : System.Web.UI.Page {
        
        protected FlashGateway.Flash Flash1; 
        
        private void Page_Load(object sender, System.EventArgs e) {
            //first
            we check if the flash sended any data if it did go on. 
            if(Flash1.Params.Count >  0 )
            {
                // we get the data from the flash servercomponent
                string sUsername = Flash1.Params[0].ToString();
                string sPassword = Flash1.Params[1].ToString();
                
                // we check if the username and password is correct
                if( sUsername == "user" && sPassword == "pass" )
                {
                    // and we return the the answer
                    Flash1.Result = "Welcome Pelle Web !"; 
                }
                else
                {
                    //return error code or text
                    Flash1.Result = "Wrong Username or password";
                }
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET 
            // Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <SUMMARY>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </SUMMARY>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}

Now we go for the Flash file. The Flash is a little special first when we add the inited function for connecting to the .NET application and it creates an object called "flashService" that you can use to connect to other classes.

Then you have to make three functions in ActionScript to call the logic in .NET.

C#
// for Flash remoting
#include "NetServices.as"

// for flash debugging
#include "NetDebug.as"

// init function
function init()
{
    if(this.inited != undefined)
    {
        return;
    }
    else
    {
        this.inited = true;
        //the url to the .NET application
        NetServices.setDefaultGatewayUrl(
          "http://localhost/FlashRemotingTest/gateway.aspx");
        //makes a connection
        gatewayConnection = 
             NetServices.createGatewayConnection();
        
        // fixing the mapping of the application
        var services = "FlashRemotingTest";
        flashService = 
             gatewayConnection.getService(services, this);
    }
}

//init the remoting        
init();

//use the flashservice object and then the filename of 
//the .netpage in this case webform1.aspx looks like this
//remember that the function name webform1 have to be the 
//same name so in this case it chould be called login.        
function webform1(username, password){
    flashService.webform1(username, password);
}

//then make one function for the result of the funtion login 
//this is the function where the result will retun to
function webform1_Result(result){
    trace("result");
}

// then make the last function for the remoting this 
// one is for the exception if it uccurs
function webform1_Status(error){
    trace(error.descripton);
}

Now just add the function webform1() to the button. Download my code and you will get the Flash file and the .NET code.

Points of interest

When it works it works really cool and fast.

History

I will do one example using datasets directly into the Flash later on.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Värderingsdata
Sweden Sweden
Systemdeveloper at Värderingsdata in Sweden

Comments and Discussions

 
QuestionPublishing flash remoting with asp.net Pin
deepak karma5-Dec-08 17:36
deepak karma5-Dec-08 17:36 
QuestionFunction calling in flash from asp.net page Flash Remoting with Asp.net Pin
deepak karma18-Nov-08 17:44
deepak karma18-Nov-08 17:44 
QuestionFlash Remoting Pin
deepakkarma18-Nov-08 17:17
deepakkarma18-Nov-08 17:17 
I am using Flash Remoting with asp.net . But the problem is that i have to named function same as the webpage. if i want to use other function with different name .so i am unable and it return message "no such services with this xyz...".pls help me how can i write function in aspx page and using such function in flash file. The function name should not be same as the name of aspx file.
QuestionSession State Pin
a_eqap22-May-07 22:19
a_eqap22-May-07 22:19 
Generalabout directory mapping Pin
meir_rivkin28-Feb-07 2:58
meir_rivkin28-Feb-07 2:58 
GeneralRe: about directory mapping Pin
OrakAManok16-Apr-07 23:04
OrakAManok16-Apr-07 23:04 
GeneralActionScript Issue Pin
callsaiprasad13-Jun-06 5:20
callsaiprasad13-Jun-06 5:20 
Generalmultiple functions Pin
ownersbox8-Jul-05 4:49
ownersbox8-Jul-05 4:49 
General... VC++.NET with Flashremoting Pin
FlyingDancer6-Jul-05 17:52
FlyingDancer6-Jul-05 17:52 
GeneralRe: ... VC++.NET with Flashremoting Pin
daniel_nilsson6-Jul-05 20:46
daniel_nilsson6-Jul-05 20:46 
GeneralRe: ... VC++.NET with Flashremoting Pin
FlyingDancer7-Jul-05 2:55
FlyingDancer7-Jul-05 2:55 
GeneralLink fixed Pin
daniel_nilsson3-Jul-05 10:23
daniel_nilsson3-Jul-05 10:23 
GeneralBad link to project code Pin
fwsouthern1-Jul-05 9:23
fwsouthern1-Jul-05 9:23 
GeneralRe: Bad link to project code Pin
M. Borsalino3-Jul-05 3:12
M. Borsalino3-Jul-05 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.