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
{
public class WebForm1 : System.Web.UI.Page {
protected FlashGateway.Flash Flash1;
private void Page_Load(object sender, System.EventArgs e) {
we check if the flash sended any data if it did go on.
if(Flash1.Params.Count > 0 )
{
string sUsername = Flash1.Params[0].ToString();
string sPassword = Flash1.Params[1].ToString();
if( sUsername == "user" && sPassword == "pass" )
{
Flash1.Result = "Welcome Pelle Web !";
}
else
{
Flash1.Result = "Wrong Username or password";
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
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.
#include "NetServices.as"
#include "NetDebug.as"
function init()
{
if(this.inited != undefined)
{
return;
}
else
{
this.inited = true;
NetServices.setDefaultGatewayUrl(
"http://localhost/FlashRemotingTest/gateway.aspx");
gatewayConnection =
NetServices.createGatewayConnection();
var services = "FlashRemotingTest";
flashService =
gatewayConnection.getService(services, this);
}
}
init();
function webform1(username, password){
flashService.webform1(username, password);
}
function webform1_Result(result){
trace("result");
}
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.