Click here to Skip to main content
Click here to Skip to main content

Flash and .NET with FlashRemoting

, 1 Jul 2005
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

daniel_nilsson
Architect Värderingsdata
Sweden Sweden
Systemdeveloper at Värderingsdata in Sweden

Comments and Discussions

 
QuestionPublishing flash remoting with asp.net Pinmemberdeepak karma5-Dec-08 18:36 
QuestionFunction calling in flash from asp.net page Flash Remoting with Asp.net Pinmemberdeepak karma18-Nov-08 18:44 
QuestionFlash Remoting Pinmemberdeepakkarma18-Nov-08 18:17 
QuestionSession State Pinmembera_eqap22-May-07 23:19 
Generalabout directory mapping Pinmembermeir_rivkin28-Feb-07 3:58 
GeneralRe: about directory mapping PinmemberOrakAManok17-Apr-07 0:04 
GeneralActionScript Issue Pinmembercallsaiprasad13-Jun-06 6:20 
Generalmultiple functions PinmemberOwnersbox8-Jul-05 5:49 
General... VC++.NET with Flashremoting PinmemberFlyingDancer6-Jul-05 18:52 
GeneralRe: ... VC++.NET with Flashremoting Pinmemberdaniel_nilsson6-Jul-05 21:46 
GeneralRe: ... VC++.NET with Flashremoting PinmemberFlyingDancer7-Jul-05 3:55 
GeneralLink fixed Pinmemberdaniel_nilsson3-Jul-05 11:23 
GeneralBad link to project code Pinmemberfwsouthern1-Jul-05 10:23 
GeneralRe: Bad link to project code PinsussM. Borsalino3-Jul-05 4:12 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 1 Jul 2005
Article Copyright 2005 by daniel_nilsson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid