`
insertyou
  • 浏览: 868184 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Flex与.NET互操作(十四):FluorineFx的AMF(Action Message Format)通信

阅读更多

AMF(Action Message Format)在开发Flash/Flex应用中使用频率是非常高的,相对普通的HTTP、WebService的SOAP等多种数据通信方式的效率更高,有人曾经做过这方面的测试,详细可以访问:http://xinsync.xju.edu.cn/index.php/archives/2162。本文将结合FluorineFx来提供通信服务接口,在客户端通过Flex来访问,简单的介绍下关于使用FluorineFx的AMF(Action Message Format)协议通信的用法。

首先建立一个FluorineFx服务库,并建立一个数据传输对象(DTO),为该对象添加[FluorineFx.TransferObject]表示该对象可以用作于FluorineFx的数据传输对象,这个对象将会在本文后面用到,如下代码块:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceFxDotNet.Services.DTO
{
[FluorineFx.TransferObject]
publicclassBook
{
publicintID{get;set;}
publicstringName{get;set;}
publicstringAuthor{get;set;}
publicdoublePrice{get;set;}

publicBook()
{}

publicBook(intid,stringname,stringauthor,doubleprice)
{
this.ID=id;
this.Name=name;
this.Author=author;
this.Price=price;
}
}
}

接下来就需要提供一个FluorineFx的远程服务(即标记有[RemotingService]的对象),通过该服务提供对外访问的方法接口,如下代码块:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceFxDotNet.Services
{
[RemotingService]
publicclassDataServices
{
publicDataServices()
{
}

///<summary>
///获取服务端的系统时间
///</summary>
///<returns></returns>
publicstringGetServerTime()
{
returnDateTime.Now.ToString();
}

publicArrayCollectionGetBooks()
{
ArrayCollectionarray
=newArrayCollection();
array.Add(
newBook(1,"三国演义","罗贯中",100.00));
array.Add(
newBook(2,"西游记","吴承恩",200.00));
array.Add(
newBook(3,"水浒传","施耐庵",300.00));
array.Add(
newBook(4,"红楼梦","曹雪芹",400.00));

returnarray;
}
}
}


通过FluorineFx网站来宿主该远程服务,通过Flex的配置文件services-config.xml配置远程访问的AMF连接通道。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><channels>
<channel-definitionid="my-amf"class="mx.messaging.channels.AMFChannel">
<endpointuri="http://{server.name}:{server.port}/{context.root}/Gateway.aspx"class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
</properties>
</channel-definition>
</channels>

FluorineFx提供的远程服务(RemotingService),使用Flex、Flash或Silverlight开发的客户端都是可以访问的,要实现客户端的远程调用其实是很简单的,这里以Flex作为示例进行演示,设置通信协议为AMF3,然后直接调用当前连接到服务器端连接对象的call()方法就可以实现远程调用,详细请查看下面完整的Flex示例。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?xmlversion="1.0"encoding="utf-8"?>
<mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"creationComplete="init()"fontSize="12">
<mx:Script>
<![CDATA[
importmx.collections.ArrayCollection;
importmx.controls.List;
importmx.rpc.events.FaultEvent;
importmx.rpc.events.ResultEvent;
privatevarnc:NetConnection;
privatevarrs:Responder;

privatefunctioninit():void
{
nc
=newNetConnection();
rs
=newResponder(onResult,onStatus);

nc.objectEncoding
=ObjectEncoding.AMF3;
nc.connect(
"http://localhost:2311/FxDotNet.Web/Gateway.aspx");
nc.client
=this;
}

privatefunctiononResult(result:String):void
{
this.lbServerTime.text="服务端系统时间为:"+result;
}

privatefunctiononStatus(event:Object):void
{
trace(
"Error");
}

privatefunctiongetServerTime(event:MouseEvent):void
{
//服务器端所提供的RemotingService的全限定名
nc.call("FxDotNet.Services.DataServices.GetServerTime",rs);
}
]]
>
</mx:Script>
<mx:Buttonx="19"y="28"label="获取服务器系统时间"click="getServerTime(event)"/>
<mx:Labelx="19"y="60"width="402"id="lbServerTime"/>
</mx:Application>

同样可以使用AMF来做大数据的传输,比如要传递一个集合、数组、DataTable或是DataSet等,下面以集合做为示例演示,ActionScript 3.0中新增了ArrayCollectin类,FluorineFx在服务器端对ActionScript 3.0的ArrayCollection对进行了序列化映射封装,服务端FluorineFx.AMF3.ArrayCollection类型的对象可以直接被客户端的ArrayCollection所接收。

如上面FluorineFx所提供的远程服务接口中的GetBooks()方法,Flex调用此方法就会得到一个ArrayCollection对象,通过调试跟踪返回的数据可以很清楚的分析这些数据,如下图示:

详细请看下面的Flex完整示例代码:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?xmlversion="1.0"encoding="utf-8"?>
<mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"fontSize="12"
width
="541"height="302"creationComplete="init()">
<mx:Script>
<![CDATA[
importmx.collections.ArrayCollection;
importmx.controls.List;
importmx.rpc.events.FaultEvent;
importmx.rpc.events.ResultEvent;
privatevarnc:NetConnection;
privatevarrs:Responder;

privatefunctioninit():void
{
nc
=newNetConnection();
rs
=newResponder(onResult,onFault);

nc.objectEncoding
=ObjectEncoding.AMF3;
nc.connect(
"http://localhost:2311/FxDotNet.Web/Gateway.aspx");
nc.client
=this;
}

privatefunctiongetBook(event:MouseEvent):void
{
nc.call(
"FxDotNet.Services.DataServices.GetBooks",rs);
}

privatefunctiononResult(result:ArrayCollection):void
{
this.bookGrid.dataProvider=result;
}

privatefunctiononFault(event:Object):void
{
trace(
"Error");
}
]]
>
</mx:Script>

<mx:Buttonx="44"y="46"label="获取图书信息"click="getBook(event)"/>
<mx:DataGridx="44"y="78"id="bookGrid">
<mx:columns>
<mx:DataGridColumnheaderText="编号"dataField="ID"/>
<mx:DataGridColumnheaderText="书名"dataField="Name"/>
<mx:DataGridColumnheaderText="作者"dataField="Author"/>
<mx:DataGridColumnheaderText="价格"dataField="Price"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>

除了使用FluorineFx所提供的ArrayCollection外,同样可以使用我们熟悉的Object[],List<Object>,DataTable等常用类型来作为数据返回的类型,其中Object[]和List<Object>类型的数据返回到客户段能够直接被客户端所接收,如下使用Object[]作数据返回类型的代码块:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->/*************服务端方法*************/
publicBook[]GetBookArray()
{
Book[]book
=newBook[]
{
newBook(1,"三国演义","罗贯中",100.00),
newBook(2,"西游记","吴承恩",200.00),
newBook(3,"水浒传","施耐庵",300.00),
newBook(4,"红楼梦","曹雪芹",400.00)
};
returnbook;
}
/*************客户端方法*************/
privatefunctiononResult(result:Array):void
{
//
}

下面是以List<Object>类型返回的代码示例代码,客户段可以使用ArrayCollection直接接收:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->/*************服务端方法*************/
///<summary>
///以泛型类型返回给客户端
///</summary>
///<returns></returns>
publicList<Book>GetBookList()
{
List
<Book>list=newList<Book>
{
newBook(1,"三国演义","罗贯中",100.00),
newBook(2,"西游记","吴承恩",200.00),
newBook(3,"水浒传","施耐庵",300.00),
newBook(4,"红楼梦","曹雪芹",400.00)
};
returnlist;
}
/*************客户端方法*************/
privatefunctiononResult(result:ArrayCollection):void
{
//
}

如果要使用DataTable类型做方法的数据返回类型就需要注意下,服务器端直接返回DataTable类型的数据客户端是不能直接接收解析的,要使客户断方便解析所返回的DataTable类型数据,FluorineFx提供了DataTableTypeAttribute,为返回DataTable类型的方法加上DataTableTypeAttribute后返回给客户端,客户端就可以直接使用ArrayCollection接收了。

返回DataTable类型
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->/******************服务端方法***********************/
[DataTableType(
"Book","FxDotNet.Services.DTO.Book")]
publicDataTableGetBookTable()
{
DataTabledt
=newDataTable("Book");
dt.Columns.Add(
"ID",typeof(int));
dt.Columns.Add(
"Name",typeof(string));
dt.Columns.Add(
"Author",typeof(string));
dt.Columns.Add(
"Price",typeof(double));

DataRowdr
=dt.NewRow();
dr[
"ID"]=1;
dr[
"Name"]="《三国演义》";
dr[
"Author"]="罗贯中";
dr[
"Price"]=100.00;
dt.Rows.Add(dr);

dr
=dt.NewRow();
dr[
"ID"]=2;
dr[
"Name"]="《西游记》";
dr[
"Author"]="吴承恩";
dr[
"Price"]=200.00;
dt.Rows.Add(dr);

dr
=dt.NewRow();
dr[
"ID"]=3;
dr[
"Name"]="《水浒传》";
dr[
"Author"]="施耐庵";
dr[
"Price"]=300.00;
dt.Rows.Add(dr);

dr
=dt.NewRow();
dr[
"ID"]=4;
dr[
"Name"]="《红楼梦》";
dr[
"Author"]="曹雪芹";
dr[
"Price"]=400.00;
dt.Rows.Add(dr);

returndt;
}
/******************客户端方法***********************/
privatefunctiononResult(result:ArrayCollection):void
{
//doother
}

关于FluorineFx的AMF就简单介绍这些,要想了解更多的Flex客户端与.NET服务器端通信,可以访问这里

本文示例代码下载 FluorineFxAMF.rar

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics