[转]flashremoting实践(二)-hello world for .net

下面来教大家在.net环境下写最简单的hello world,好了闲话少说,现在开始:

这里我假设你已经看过我第一篇关于配置的文章,并已经成功的执行了前两个例子。

1.首先请大家进入你的虚拟目录c:\Inetpub\wwwroot,在这里你会看到flashremoting文件夹,也就是例子文件的文件夹,在这个目录新建一个myASPApp文件夹,这里我们开发的程序就放到这个文件夹下,在这里我们新建一个名为bin的目录,建好之后请回到c:\Inetpub\wwwroot,进入flashremoting目录,把这里的bin目录下的flashgateway.dll和frconfig.txt文件拷贝到myASPApp下的bin目录里,然后在把flashremoting下的gateway.aspx和web.config两个文件拷贝到myASPApp目录下,在这里我不具体介绍为什么我们要把这些文件拷贝到相应的目录里,如果你想了解请看自带的帮助文件remoting支持4种.net技术分别是:
asp.net
ado.net data-binding
web services
assembly(with the .dll extension)
要让我们写的remoting程序能够找到asp.net页你就必须把asp.net页放到虚拟目录下或者是子目录下,也就是我们为了区分我们要开发的项目所以我们在虚拟目录下建了一个myASPApp文件夹,我们把我们需要的asp.net放到这里就可以了。好了,下面我们开始写程序了。

2.在myASPApp下建立一个.aspx文件,也就是asp.net文件取名为helloWorldNET.aspx
首先要把你用的是什么语言做声明,我这里用的是vb.net写法是

None.gif < %@ Page language = " vb "  debug = " true "  % >

为了从flash应用程序中调用数据,或者在asp.net页面返回结果给flash,你要在asp.net页面上使用flash remoting定制服务器端控制这个控制是又flashgateway.dll所提供的,也就是拷贝到bin目录里的flashgateway.dll,你必须在程序代码之前首先注册这个控制,方法是这段代码
 
ExpandedBlockStart.gif ContractedBlock.gif < %@ Register TagPrefix = " Macromedia "   Namespace = Namespace="FlashGateway" Assembly="flashgateway" %>

这个注册说明了建立标识符前缀Macromedia,和命名空间FlashGateway,以及提供功能的动态连接库文件flashgateway注册之后你就可以使用它传递数据给flash应用程序了比如这样
 
None.gif < Macromedia:Flash ID = " Flash "  runat = " server " >  
None.gif  Hello from .NET! 
None.gif
</ Macromedia:Flash >  

这里我个人认为,在<Macromedia:Flash ID="Flash" runat="server"></Macromedia:Flash>之间写的任何代码都会被做为返回值传递给flash,比较像.net中的response.wirte()和java中的System.out.print();说到这里大家可能已经会写这个程序了,不错上面的代码就是hello world程序,但是这里我不提倡大家这样写,还有另一种写法,在注册完之后直接写这样的代码<Macromedia:Flash ID="Flash" runat="server"/>,然后在下面我们用asp.net的正常写法

None.gif < script runat = " server " >  
ExpandedBlockStart.gifContractedBlock.gif  
Sub page_load() sub page_load(sender as object,e as eventargs) 
InBlock.gif    flash.result
="hello world!!!" 
ExpandedBlockEnd.gif  
end sub
 
None.gif
</ script >
None.gif

这里我们用page_load也就是页面加载时所执行的程序,这样,当这个页面加载时就会把hello world直接传递给flash对象的result方法,这是我们在flash里接收的值的方法,好了asp.net页面已经写完了,下面看一下完整的代码

None.gif < %@ Page language = " vb "  debug = " true "  % >  
ExpandedBlockStart.gifContractedBlock.gif
< %@ Register TagPrefix = " Macromedia "   Namespace = Namespace="FlashGateway" Assembly="flashgateway" %> 
InBlock.gif
<Macromedia:Flash ID="Flash" runat="server"/> 
InBlock.gif
<script runat="server"> 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
Sub page_load()sub page_load(sender as object,e as eventargs) 
InBlock.gif  flash.result
="hello world!!!" 
ExpandedSubBlockEnd.gif  
end sub
 
InBlock.gif
</script> 

3.下面是我们所要写的flash端的代码,这里首先要把remoting类导入到flash中,方法很简单选择菜单栏,窗口--其他面板--公用库--remoting,这时你会看到界面右边出现remoting的库面板,把RemotingClasses拖到场景中在删除,这样RemotingClasses类就会被添加到我们的文件的库里面,程序执行的时候它就被导入到程序里了,如果你想在NetConnection Debugger面板里调试程序,那么你需要把remoting类库里的RemotingDebugClasses也拖到场景中,好了现在开始写as程序

4.拖一个textinput组件到主场景中,取名为messageDisplay_txt,然后选择主场景第一真打开动作面板,首先需要导入一些需要的类代码如下

None.gif import mx.remoting.Service; 
None.gifimport mx.rpc.RelayResponder;
None.gifimport mx.rpc.FaultEvent; 
None.gifimport mx.rpc.ResultEvent; 
None.gifimport mx.remoting.PendingCall;

之后,第一步:我们要连接服务器所以创建一个service对象代码如下:

None.gif var howdyService:Service  =   new  Service( " http://localhost/myASPApp/gateway.aspx " ,
None.gif          null, 
" myASPApp " , null, null); 

第一个参数 http://localhost/myASPApp/gateway.a...pp.helloworld。

第二步:呼叫服务器端的helloworld方法

None.gif var pc:PendingCall  =  howdyService.helloWorldNET();

这里你会看到呼叫的服务器端的方法其实就是asp.net页helloWorldNET.aspx的名字

第三步:需要写呼叫方法后服务器返回的结果和或者是失败的方法

None.gif pc.responder  =   new  RelayResponder(this,  " serviceFunctionName_Result " " serviceFunctionName_Fault " ); 

这里如果呼叫成功那么执行我们自定义的serviceFunctionName_Result方法,如果呼叫失败那么执行我们自定义的serviceFunctionName_Fault方法。
然后我们要写自定义的两个方法,首先是serviceFunctionName_Result方法,代码如下:

ExpandedBlockStart.gif ContractedBlock.gif Function serviceFunctionName_Result() function serviceFunctionName_Result(result:ResultEvent) { 
InBlock.gif  
// display successful result 
InBlock.gif  messageDisplay_txt.text 
= result.result; 
InBlock.gif

这里当呼叫成功后会把服务器端的结果传递给result对象,result对象的result方法就是从服务器端得到的值,然后我们把他放到messageDisplay_txt里显示
当呼叫失败时flash会调用serviceFunctionName_Fault方法,代码如下:

ExpandedBlockStart.gif ContractedBlock.gif Function serviceFunctionName_Fault() function serviceFunctionName_Fault(fault:FaultEvent) { 
InBlock.gif  
//display fault returned from service 
InBlock.gif  messageDisplay_txt.text 
= fault.fault.faultstring; 
InBlock.gif

失败的信息,会传递给fault对象,然后用messageDisplay_txt来显示出来,好了这个程序已经写完了,完整的代码如下:

None.gif import mx.remoting.Service; 
None.gif  import mx.rpc.RelayResponder; 
None.gif  import mx.rpc.FaultEvent; 
None.gif  import mx.rpc.ResultEvent; 
None.gif  import mx.remoting.PendingCall; 
None.gif  
//  connect  to  service  and  create service  object  
None.gif  var howdyService:Service 
=   new  Service( " http://localhost/myASPApp/gateway.aspx " ,
None.gif          null, 
" myASPApp " , null, null); 
None.gif  
//   call  the service helloWorld() method 
None.gif  var pc:PendingCall 
=  howdyService.helloWorldNET(); 
None.gif  
//  tell the service what methods handle result  and  fault conditions
None.gif  pc.responder 
=   new  RelayResponder(this,  " serviceFunctionName_Result " " serviceFunctionName_Fault " );
ExpandedBlockStart.gifContractedBlock.gif  
Function serviceFunctionName_Result() function serviceFunctionName_Result(result:ResultEvent) { 
InBlock.gif  
// display successful result 
InBlock.gif    messageDisplay.text 
= result.result; 
InBlock.gif  } 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
Function serviceFunctionName_Fault()function serviceFunctionName_Fault(fault:FaultEvent) { 
InBlock.gif  
//display fault returned from service 
InBlock.gif    messageDisplay.text 
= fault.fault.faultstring; 
InBlock.gif  } 

看完之后大家是不是觉得调用.net方法其实很简单,不错,as代码方面其实写法比较固定,只要正确的找到gateway.aspx文件引导,然后正确的找到你所写的asp.net页,应该就没有什么问题。在这里我注重把as2.0的写法告诉大家,并没有对每一个对象例如Service,PendingCall等对象做细致的讲解,如果你想仔细的认识这些对象那么请看相关资料

下一节将会向大家介绍java环境下的hello world程序,我会努力的已最快速度写出来的,由于时间仓促,所以难免有写的不对的地方,如果写错了,请大家指正,如果想与我讨论remoting方面的问题的请与我联系,我的qq:22339146,msn:lwanchen@hotmail.com。

转载于:https://www.cnblogs.com/savageworld/archive/2006/07/22/457026.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值