Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Flash and .NET with FlashRemoting

0.00/5 (No votes)
1 Jul 2005 1  
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:

<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:

<%@ 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:

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.

// 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