FMS服务器端远程调用Flex客户端函数的心得
客户端程序:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="15" initialize="init()" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
//private var cn:NetConnection;
private var serverApp:String = "rtmp://localhost/TestAppla";
private var myRespoder:Responder = new Responder(onReply);
private function init():void{
btn.addEventListener(MouseEvent.CLICK,btn_click);
}
private function btn_click(evt:MouseEvent):void{
var cn:NetConnection = new NetConnection();
// cn.getNumber = function(){
// Alert.show("你好");
// };
cn.connect(serverApp);
cn.client = this; //要加上这一句话,不加上这句会出现不同步的错误
cn.addEventListener(NetStatusEvent.NET_STATUS,cn_NetStatusEvent);
cn.addEventListener(AsyncErrorEvent.ASYNC_ERROR,cn_AsyncErrorEvent);
cn.call("sayHelloWorld",myRespoder);
//cn.addEventListener(Event.ACTIVATE,cn_ACTIVATE);
//cn.addEventListener(Event.DEACTIVATE,cn_DEACTIVATE);
//cn.addEventListener(IOErrorEvent.IO_ERROR,cn_IO_ERROR);
//cn.addEventListener(SecurityErrorEvent.SECURITY_ERROR,cn_SECURITY_ERROR);
//cn.call("serverHelloMsg", myRespoder, "World"); //调用服务器端的代码
}
private function cn_NetStatusEvent(Result:NetStatusEvent):void{
Alert.show(Result.info.code);
if(Result.info.code == "NetConnection.Connect.Success"){
Alert.show("连接成功");
}else{
Alert.show("连接失败");
}
}
private function cn_AsyncErrorEvent(evt:AsyncErrorEvent):void{
//Alert.show(evt.target as String);
Alert.show("cn_AsyncErrorEvent was called");
}
private function cn_ACTIVATE():void{
Alert.show("cn_ACTIVATE was called");
}
private function cn_DEACTIVATE():void{
Alert.show("cn_DEACTIVATE was called");
}
private function cn_IO_ERROR():void{
Alert.show("cn_IO_ERROR was called");
}
private function cn_SECURITY_ERROR():void{
Alert.show("cn_SECURITY_ERROR was called");
}
//来自服务器端的回复
private function onReply(Result:Object):void{
Alert.show(Result as String);
}
//用于从服务器端调用的方法
public function showServerMsg(msg:String):void{
Alert.show("magictang");
}
public function testMessage(msg:String):void{
Alert.show(msg as String);
}
public function serverUpdate():void{
Alert.show("哥开始调用你们了");
}
]]>
</mx:Script>
<mx:Button x="316" y="433" id="btn" label="Connect"/>
<mx:TextInput x="247.5" y="255" width="300" id="result"/>
</mx:Application>
服务器端main.asc的代码
//定义用户的列表
userList=[];
application.onAppStart=function()
{
trace("fms set up server......");
trace("magictanghehe.......");
}
application.onConnect = function(currentClient){
application.acceptConnection(currentClient);
currentClient.call("showServerMsg",null,currentClient.username+"welecome!");
currentClient.username = currentClient.id;
trace(currentClient.username+"加入聊天室");
userList.push(currentClient.username);
//向每个在线的用户发送我来了的消息
//
currentClient.sayHelloWorld = function(){
return "I Say hello to "+currentClient.username;
}
trace("当前用户列表"+userList);
}
application.onDisconnect = function(currentClient){
trace("user "+currentClient.username+" live chatroom");
//将进入的用户删除掉
for(j=0;j<userList.length;j++){
if(userList[j] == currentClient.username)
{
userList.splice(j,1);
}
}
trace("当前用户列表"+userList);
}
注意此例子的核心就是currentClient.call("showServerMsg",null,currentClient.username+"welecome!"); 是服务器端远程调用客户端的代码。官方对该函数的提供的例子:
nc = new NetConnection();
nc.getNumber = function(){
return (Math.random());
};
nc.connect("rtmp:/clientCall");
我在Flex中调试不过。可能在Flash cs3中的as2.0 as3.0能调试通过。
但是在Flex中cn.client = this; 起到的作用相当于上述的作用。
没有上面的一句话程序怎么调试都不会正确。
就是这一句话调试了半天。
真是所谓 只要功夫深 铁杵磨成针
浙公网安备 33010602011771号