信息之舟电脑软件应用栏目编者按:
二、Flash Remoting Actionscript
1、在FLASH中应用Flash Remoting需要那类的Actionscript?
如果连接到应用服务器,就必须在Flash电影的第一帧加上FlashRemoting包含Actionscript类的指令。代码如下:
#i nclude "NetServices.as"
#i nclude "NetDebug.as"
#i nclude "DataGlue.as"
include为Flash Remoting指令,为相关Actionscript类就提供了在客户端连接、交互和管理数据的能力。
连接FlashRemoting服务器、调用服务提供的函数,需要使用NetServices
调试FlashRemotin应用程序,需要使用NetDebug,他所提供的所有相关服务将在FlashMovie生成前被删除。
DataGlue函数可以有效的将一个记录集对象绑定到FlashMX UIs组件上
2、怎样实现FLASH与应用服务器的通信?
利用Remoting实现flash与服务器通信,必须在flash电影中利用NetServices函数createGatewayConnection和getService建立一个连接服务对象。下列的代码例子建立一被称为myService的服务对象。
#i nclude "NetServices.as"
if (inited == null)
{
// if条件语句 如果inited没有赋值,那么变量inited值就为真
// Services.setDefaultGatewayUrl函数提供了FlashRemoting服务URL
NetServices.setDefaultGatewayUrl("http://localhost:8100/flashservices/gateway");
// 连接 gateway
gateway_conn = NetServices.createGatewayConnection();
// 创建了FlashempireService服务对象。这个服务名称为myService,指向Web root上my目录下的一个名service的Cf组件。需要注意的是目录路径的划界线不是反斜杠
myService = gateway_conn.getService("my.service", this);
}
在这个例子中,使用setDefaultGatewayUrl函数在flash电影中指定了falshRemoting所有服务通信的URL。一旦使用setDefaultGatewayURL函数,就不需要再为每一个createGatewayConnection函数提供URL,这对于在FlashMX创作环境中进行开发时特别有用的。建立服务连接对象后,你就像下面的例子一样同应用服务器通信。
myService.remoteServiceMethodName(param1, "param2");
3、 flashs应用服务网关URL是指向我服务器中目录上的什么地方?
Flash Remoting服务网关的url(如:"http://localhost:8100/flashservices/gateway")是不存在的,仅仅是一个映射而已。不要花心思去寻找 wwwroot下面的指向目录。
4、怎样处理服务器上返回的数据?
调用服务对象中的函数,使用服务对象名后跟随一个函数名,如下例:
myService.getTemperature("New York");
在这个例子中,getTemperaturn函数作为一个公开的方法或者应用程序页面存在于应用服务器中,该函数传递了一个字符串参数,“New York”,要传递多个参数到Service函数,需要包含一个Service函数调用的逗号分割值的列表,例如:
server.getTemperature("New York", 1998, "average");
在这个例子里,service函数传递了三个参数,城市(“New York”),年(1998)以及指令(“average”)。
注意:参数必须按照service函数要求的顺序出现。
5、怎样在FLASHMX UI组件上显示服务器返回的数据结果?
要容易地显示记录集对象的内容,使用 Flash MX用户界面( UI)组件ListBox和ComboBox。对任一UI组件,使用DataGlue函数,包括bindformatstrings和bindformatfunction。 DataGlue函数绑定一个数据提供者到一个数据用户,比如绑定记录集对象到combobox组件。 数据作为标准的label/data配对出现。
DataGlue函数并不对原始数据提供者的数据进行复制,不过,数据是在数据用户需要时从原始数据供应者处取出, 该数据用户可以使用数据像任何其他的参数。每个记录的标签是用来显示纪录的文本,每个记录的data是由该数据用户的getValue函数返回的value,bindFormatStrings函数让你格式化在函数调用中的数据。 例如∶DataGlue.bindFormatStrings(myComboBox, myRecordSet, "#parkname# ?(#parktype#)", "#city#, #state# #zipcode#");
在这个例子中, myComboBox表示Flash电影中的一个combobox组件, myRecordSet表示RecordSet对象。 parkname, parktype, city, state,和zipcode变量表示记录字段名称。 Flash电影显示了parkname和parktype变量。 city, state,和zipcode变量在用户选择该记录的时候返回。 你可以使用getValue函数回送这些变量值。
bindformatstrings函数让你调用一个函数去格式化数据。 你可以如下例所示创建该函数:
function myFormatFunction ( record )
{
// the label is the parkname record field, translated to lower case
var theLabel = record.parkname.toLowerCase();
// the data is the length of the parkname record field
var theData = record.parkname.length;
// return the label and value to the caller
return {label: theLabel, data: theData};
}
//call the bindFormatFunction
DataGlue.bindFormatFunction(dataView2, result, myFormatFunction);
在这个例子中, record表示一记录集对象, dataview2表示Flash电影中的用户界面组件。
