.Net和Flash间的通信

Flash调用页面方法
C#的类声明:
using System;
using System.Collections.Generic;
using System.Text;
namespace TestFlash
{
    public class TestFlash
    {
        public string Test()
        {
          return "test...";
        }        
    }   
}

FLASH调用方法:
import mx.remoting.*;
import mx.rpc.*;
function InvoteGateway()
{
  var GatewayURL:String = "http://127.0.0.1/flashremoting/gateway.aspx"; //flash网关地址
  var ClassURL:String = "TestFlash.TestFlash";// 命名空间和类名
  var FunctionName:String = "Test";// 方法名
  var newService:Service = new Service(GatewayURL, null, ClassURL, null, null);
  var pc:pendingCall = eval("newService."+FunctionName)(obj);
  pc.responder = new RelayResponder(this, "onRemotingResult", "onRemotingFault"); 
}
function onRemotingResult(re:ResultEvent)
{
  txtResult.text = re.result;// 值则为test...
}
function onRemotingFault(fault:FaultEvent)
{
  trace(fault.fault.faultstring);
}
run.addEventListener("click",InvoteGateway());

运行环境配置:
1.        配置.NET运行环境:
安装Dotnet Framework 2.0版本
2.        配置Flashremoting运行环境:
最好首先安装Flash8,然后安装flashremoting-net-win-en2.exe,最后安装flashremoting_components_flash8.msi.




flash调用webservices
Flash作为UI层,有下列好处:
1、 不依赖于具体的平台
2、 轻量级,可以轻易的构造富客户端程序。
3、 可以给用户更好的体验。

但是,Flash在构建复杂的应用时,无法直接与数据库进行交互,必须依赖于其他的中间层进行,比如FlashRemtoing、PHP等等。各种方式都有优缺点。
在Flash与Asp.net进行交互时,最好的方式就是使用WebService。

下面构造第一个最简单的例子,从服务器端开始:
<%@ WebService Language="c#" Class="wsLearn.test" %>
//这句话声明了这是一个WebService。
using System;
using System.Web;
using System.Web.Services;
//创建WS时必须引用的类。
namespace wsLearn{
//ws “test”所在的命名空间
[WebService(Namespace="http://www.dxlschool.com/ws")]
public class test:System.Web.Services.WebService{
public test(){
//do nothing
}
[WebMethod]
public string hello(){
return "你好!!";
}

就这样,一个最简单的WS构造好了,他只包含一个方法hello,把它放到你的支持asp.net的虚拟目录下,在浏览器中输入地址可以查看该ws运行的状况。
比如我在IE中输入如下地址http://localhost/ws/test.asmx
IE中会给出下列提示:

test
支持下列操作。有关正式定义,请查看服务说明。
Hello

点击”hello”,ws会返回一段XML文件。

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.dxlschool.com/ws">你好!!</string>

下面我们在Flash中调用这个hello方法。
我使用的是Web 服务类来调用该ws 的hello方法。
/**
ActionScript Source File -- Created with SAPIEN Technologies PrimalScript 3.0

@class wsSessionTest_1
@package wsSessionTest_1.as
@author JimLee
@codehint
@example
@tooltip
*/

import mx.services.*;
//导入services包

var myws:WebService= new WebService("http://localhost/ws/test.asmx?wsdl");
//使用WebService对象myws引用ws

//定义ws方法hello回调后的处理,参数result是ws的hello方法返回的值
// showSession_lb:lable组件
function hello(result) {
showSession_lb.text="从ws返回的消息:"+result;
};

//testSession_bt:按钮
//使用PengingCall对象op_1在flash中代理ws的hello方法
this.testSession_bt.onPress=function(){
var op_1:PendingCall=myws. hello ();
op_1.onResult= hello;
}

需要注意的是,如果Web 服务类仅限 Flash Professional中使用,如果要在作品中使用Web服务类,必须导入Web服务类包,不然,是不会有响应的。
依次点击“窗口-其他面板-公用库-类”,打开公用库“类”,从中拖动WebSessionClasses组件到舞台上,将Web服务类包导入Fla,然后,你可以删除舞台上的WebSessionClasses组件的实例。

下面就可以测试一下你的swf了,如果不出以外,lable组件showSession_lb中将出现“从ws返回的消息:你好”。不过,要提醒的是:可要给你的lable组件留够足够的长度来显示信息啊!

原文地址:https://www.cnblogs.com/Magicam/p/1273272.html