专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Flex

AMFPhp 与 Flex Builder3 的交互(1)

发布时间:2011-06-27 19:45:57 文章来源:www.iduyao.cn 采编人员:星星草
AMFPhp 与 Flex Builder3 的交互(一)

好了,让我们写一个HelloWorld程序吧。首先打开amfphp\services目录,新建一个HelloWorld.php文件,在文件中输入以下代码:

<?php
class HelloWorld {
	
	function sayHello() {
		return "Hello World!";
	}
}
?>

 好啦,php端,就先这样了。然后我们写flex 端的吧。

 

在Flex builder3新建一个Flex工程。需要注意的是,在Application server type选择php 。下一步。
在web root 下填入 D:\php 即我的本地web服务的目录。root url 填入:http://127.0.0.1 即本地IP啦,呵呵。然后下一步,如果你不改源文件的目录的话,就直接Finish啦。
好,在src里再新建一个xml文档,名字叫:services-config.xml ,打开,输入以下代码,这个flex与amfphp的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service id="amfphp-flashremoting-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="amfphp">
<channels>
<channel ref="my-amfphp"/>
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
</services>
<channels>
<channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://127.0.0.1/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>

 注意endpoint uri="http://127.0.0.1/amfphp/gateway.php" 的路径 然后右键点击你的项目,选择Properties选项,在弹出的对话框里选择Flex Compiler,在Additional compiler arguments 里加入 -services "services-config.xml",如图:

点击src ,打开AMFphp.mxml,好输入以下代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF" viewSourceURL="srcview/index.html">
    <mx:RemoteObject id="myservice" fault="faultHandler(event)" showBusyCursor="true" source="HelloWorld" destination="amfphp">
        <mx:method name="sayHello" result="resultHandler(event)" />
    </mx:RemoteObject>

    <mx:Script>
        <![CDATA[
            import mx.managers.CursorManager;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            private function faultHandler(fault:FaultEvent):void
            {
                CursorManager.removeBusyCursor();
                result_text.text = "code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail;
            }

            private function resultHandler(evt:ResultEvent):void
            {
                result_text.text = evt.message.body.toString(); // same as: evt.result.toString();
            }
        ]]>
    </mx:Script>

    <mx:Button x="250" y="157" label="sayHello" width="79" click="myservice.getOperation('sayHello').send();"/>
    <mx:Button x="250" y="187" label="test fault" click="myservice.getOperation('foo').send(); "/>
    <mx:TextArea x="10" y="36" width="319" height="113" id="result_text"/>
    <mx:Label x="10" y="10" text="Result:"/>
</mx:Application>

在<mx:RemoteObject>下呢,定义的就是RemoteObject相关的一些东东.(这话题听着别扭),id这个就不解释 了,fault就是当服务调用失败并且操作自身不处理时,调度fault事件。showBusyCursor呢,如果为 true,在执行服务时就会显示忙碌光标。

在这里重点是后两个属性,source和destination。source后面写的是,你在服务器上的文件名,也就是咱们用php写的那个类的文件的名字,即在D:\php\amfphp\services目录下,这个算是咱们本地服务的根目录。如果你要建一 个新的目录为hello,然后把HelloWorld.php这个文件放进去,那么我们的程序应该改一下,即source = "hello.HelloWorld" 。destination有代表什么呢?服务的目标。这个值应该与services-config.xml文件中的目标条目匹配,那好,去找一下 services-config.xml这个文件吧,看看有这个叫做destination这个东东吗?该 <mx:method />这行了,很简单,这个就是咱们要用的方法啊,名字和php里面方法的名字要一致哦。
另外,需要注意的是在我们的显示界面的代码中,在label为sayHello的<mx:Button />里面,click = "myservice.getOperation('sayHello').send" ,这行代码的意思就是当我们点击按钮的时候,将要执行我们的remoteObject对像中的sayHello方法,并且等待result事件,如果一切 顺利,php端会给我们返回一个叫做"Hello world"的字符串,然后,将去调用<![CDATA  .....  ]]>里面的ResultHandler函数,接着我们的 result_text文本就有任务啦,那就是显示这个字符串。怎么样,很简单吧。(其他的请看源代码注释,我也是刚研究,有错误的地方请指正)

 

好,先不管代码的含义,我们运行一下吧http://127.0.0.1:/amfphp/项目名/bin-debug/hello.html,如果你看见了下面的图片 ,你就成功了。点击sayHello按钮 ,看见它和你问好了吗?

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: