Flex中的远程对象访问,也就是服务端提供一个远程服务对象(RemotingService Object),在Flex客户端通过相应的访问技术去调用远程对象的过程。
在本系列文章的前面几篇文章中所介绍的访问Webservice的方法,也就是一种远程对象方法,只不过他是基于WEB服务(WebServie)的远程访问,不是基于远程对象(Remoting Object)的的远程访问。要想直接实现基于对象的远程访问是比较麻烦的,然后FluorineFx则专门为我们提供了该功能,通过FluorineFx的核心库来开发远程对象(Remoting Object)服务,具体是怎么实现的呢?FluorineFx要求为远程对象提供[RemotingService]标记来提供远程对象服务,看看下面的RemotingServiceAttribute的详细定义:
1
[AttributeUsage(AttributeTargets.Class, AllowMultiple
=
false
)]
2 public sealed class RemotingServiceAttribute : Attribute
3 {
4 public RemotingServiceAttribute();
5 public RemotingServiceAttribute( string serviceName);
6 }
2 public sealed class RemotingServiceAttribute : Attribute
3 {
4 public RemotingServiceAttribute();
5 public RemotingServiceAttribute( string serviceName);
6 }
从上一篇文章中的示例代码可以看出,使用.NET(c#)定义了一个Sample的远程对象服务类,并为其指定了[RemotingService],详细如下:
1
[RemotingService(
"
Fluorine sample service
"
)]
2 public class Sample
3 {
4 public Sample()
5 {
6 }
7
8 public string Echo( string text)
9 {
10 return " Gateway echo: " + text;
11 }
12 }
2 public class Sample
3 {
4 public Sample()
5 {
6 }
7
8 public string Echo( string text)
9 {
10 return " Gateway echo: " + text;
11 }
12 }
从上一篇文章中搭建FluorineFx与.NET的开发环境过程中就已经出现过Flex客户端调用FluorineFx的远程对象示例,下面我们在来看看这个示例:
1
<
mx:RemoteObject id
=
"
service
"
destination
=
"
fluorine
"
2 source = " FlexDotNet.ServiceLibrary.Sample " >
3 < mx:method name = " Echo " result = " onResult(event) " >
4 </ mx:method >
5 </ mx:RemoteObject >
2 source = " FlexDotNet.ServiceLibrary.Sample " >
3 < mx:method name = " Echo " result = " onResult(event) " >
4 </ mx:method >
5 </ mx:RemoteObject >
1
<
mx:Script
>
2 <! [CDATA[
3 import mx.rpc.events.ResultEvent;
4 internal function onClick(): void
5 {
6 service.Echo(txtInput.text);
7 }
8
9 internal function onResult(evt:ResultEvent): void
10 {
11 txtResult.text = evt.result.toString();
12 }
13 ]] >
14 </ mx:Script >
2 <! [CDATA[
3 import mx.rpc.events.ResultEvent;
4 internal function onClick(): void
5 {
6 service.Echo(txtInput.text);
7 }
8
9 internal function onResult(evt:ResultEvent): void
10 {
11 txtResult.text = evt.result.toString();
12 }
13 ]] >
14 </ mx:Script >
如上可实现远程对象访问,通过Flex的非可视化组件<mx:RemoteObject>进行远程对象连接。其中的source属性指定远程对象,格式为全限定名(命名空间+类名)。destination属性是非常重要的,这决定了Flex客户端是否可以正确的访问到远对象,相关配置如下:
1
<
destination id
=
"
fluorine
"
>
2 < properties >
3 < source >*</ source >
4 </ properties >
5 </ destination >
2 < properties >
3 < source >*</ source >
4 </ properties >
5 </ destination >
在<mx:RemoteObject>组件内部使用<mx:Mothod>组件配置远程对象中的方法,详细见本文前面部分。要真实实现远程对象访问的核心是对象的适配器和连接通道:
1
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
2 < service id = " remoting-service "
3 class = " flex.messaging.services.RemotingService "
4 messageTypes = " flex.messaging.messages.RemotingMessage " >
5 < adapters >
6 < adapter - definition id = " dotnet " class = " FluorineFx.Remoting.RemotingAdapter " default = " true " />
7 </ adapters >
8
9 < default - channels >
10 < channel ref = " my-amf " />
11 </ default - channels >
12
13 < destination id = " fluorine " >
14 < properties >
15 < source >*</ source >
16 </ properties >
17 </ destination >
18 </ service >
2 < service id = " remoting-service "
3 class = " flex.messaging.services.RemotingService "
4 messageTypes = " flex.messaging.messages.RemotingMessage " >
5 < adapters >
6 < adapter - definition id = " dotnet " class = " FluorineFx.Remoting.RemotingAdapter " default = " true " />
7 </ adapters >
8
9 < default - channels >
10 < channel ref = " my-amf " />
11 </ default - channels >
12
13 < destination id = " fluorine " >
14 < properties >
15 < source >*</ source >
16 </ properties >
17 </ destination >
18 </ service >
实际开发中我们可以进行自定义通信通道,默认情况下是使用FluorineFx为我们提供的默认连接通道:
1
<
channels
>
2 < channel-definition id ="my-amf" class ="mx.messaging.channels.AMFChannel" >
3 < endpoint uri ="http://{server.name}:{server.port}/{context.root}/Gateway.aspx" class ="flex.messaging.endpoints.AMFEndpoint" />
4 < properties >
5 <!-- <legacy-collection>true</legacy-collection> -->
6 </ properties >
7 </ channel-definition >
8 </ channels >
2 < channel-definition id ="my-amf" class ="mx.messaging.channels.AMFChannel" >
3 < endpoint uri ="http://{server.name}:{server.port}/{context.root}/Gateway.aspx" class ="flex.messaging.endpoints.AMFEndpoint" />
4 < properties >
5 <!-- <legacy-collection>true</legacy-collection> -->
6 </ properties >
7 </ channel-definition >
8 </ channels >
本文转自 beniao 51CTO博客,原文链接:http://blog.51cto.com/beniao/137933,如需转载请自行联系原作者