继续使用应用一中的java文件,添加方法
public boolean appConnect(IConnection conn, Object[] args) { System.out.println(" 连接"); return true; } public String change(String str){ System.out.println(" 客户端调用服务器"); return str.toUpperCase();// 传入的字符串转换成大写后返回 }
?新写flex文件,测试实现情况:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Declarations> </fx:Declarations> <fx:Script> <![CDATA[ import mx.controls.Alert; private var rtmpURL:String="rtmp://localhost:1935/mychat"; private var conn:NetConnection=new NetConnection(); private var isConnectSuccess:Boolean=false; private var responder:Responder=new Responder(resultFun); private function resultFun(object:String):void{ trace(object); result.text=object.toString(); } private function clickConnect(e:MouseEvent):void{ conn.addEventListener(NetStatusEvent.NET_STATUS,netStatus); conn.connect(rtmpURL); } private function click(e:MouseEvent):void{ invoke(); } private function invoke():void{ if(isConnectSuccess){ conn.call("change",responder,str.text);//change 是服务器端方法名称 //conn.call("change",responder,"1234146");//change 是服务器端方法名称 }else{ Alert.show("还没连接到服务器"); } } private function netStatus(e:NetStatusEvent):void{ trace(e.info.code); if(e.info.code=="NetConnection.Connect.Success"){ isConnectSuccess=true; } } ]]> </fx:Script> <mx:Button x="224" y="175" label="调用服务器方法" click="this.click(event)"/> <s:TextInput x="129" y="145" width="212" id="str" /> <s:Label x="129" y="119" text="显示从服务器端返回的字符" width="160" id="result" fontSize="12" /> <mx:Button x="129" y="175" label="连接服务器" click="this.clickConnect(event)"/> </s:Application>
?