65.9K
CodeProject 正在变化。 阅读更多。
Home

Flash 和 .NET 使用 FlashRemoting

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.27/5 (9投票s)

2005 年 7 月 1 日

2分钟阅读

viewsIcon

102458

downloadIcon

1683

使用 .NET 测试 FlashRemoting。

引言

本文解释了 FlashRemoting 在 .NET 和 Flash 中的工作原理。我们使用 FlashRemoting 实现与数据库或其他存储信息的快速安全通信,并在 Flash 电影中显示它。在这个例子中,我们不使用任何数据库,这是测试它最简单的方法。

使用代码

首先,您需要从 Macromedia 安装 FlashRemoting,并且需要安装 Macromedia Flash MX 2004 和 ActionScript 2.0。

现在我们可以开始 .NET 代码。首先,您需要在 Visual Studio 中启动一个新的 Web 应用程序项目,并将 FlashRemoting 服务器控件作为新的引用添加。您可以在 c:\inetpub\wwwroot\flashremoting\bin\flashgateway.dll 中找到它。

然后,您需要在 system.web 标签下将以下代码添加到您的 Web.config 文件中

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

现在我们可以开始“编码”。您需要创建一个初始化页面供 Flash 使用,以便 Flash 可以连接到 .NET 应用程序。只需创建一个名为“gateway.aspx”的新页面即可。

要从 Flash 获取参数并在 .NET 应用程序中发送数据到 Flash,您需要将服务器控件添加到页面中,因此请确保您的 ASPX 文件如下所示

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

现在是后台代码。要从 .NET 应用程序向 Flash 发送信息,您需要使用名为“flash1”的服务器控件。在这个示例中,我们将加载一个简单的字符串到 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
    }
}

现在我们开始 Flash 文件。Flash 有点特殊,首先我们需要添加 inited 函数来连接到 .NET 应用程序,它会创建一个名为“flashService”的对象,您可以使用该对象连接到其他类。

然后,您需要在 ActionScript 中创建三个函数来调用 .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(
          "https:///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);
}

现在只需将函数 webform1() 添加到按钮即可。下载我的代码,您将获得 Flash 文件和 .NET 代码。

关注点

当它工作时,它真的非常酷而且速度很快。

历史

我稍后会做一个使用数据集直接在 Flash 中显示的示例。

© . All rights reserved.