FMS服务器端和客户端互相调用

流程如下:
1. 客户端在连接时提供账户信息用于验证
2. 服务器端进行账户信息验证
3. 如果验证成功调用客户端的onLoginOKCBS方法将一句问候语作为onLoginOKCBS的参数传递给客户端,
    同时onLoginOKCBS返回一句问候语给服务器端
4. 如果验证失败返回账户错误信息
5. 客户端调用服务器端的getInfoFromSrvCBC方法传入id为10005的消息参数获取服务器端当前时间

服务器端main.asc完整代码:

 
var  error  =  { " details " : " Please provide the correct user name and password! " };
var  greeting = " Welcome to our humble abode! " ;

application.onConnect 
=   function (clientObj, userinfo)
{
    
if (userinfo  !=   null &&  userinfo.username == ' admin '   &&  userinfo.userpwd == ' admin '  )
    {
        application.acceptConnection(clientObj);
        
// call定义在客户端NetConnection.client对象中的onLoginOKCBS
        clientObj.call( " onLoginOKCBS " new  loginOKHandler(), greeting);
        
        
// 定义给客户端调用的方法getInfoFromSrvCBC, 
         // var msg:Object {"id":10005, "details":"GET_SRV_TIME"};
        clientObj.getInfoFromSrvCBC  =   function (msg)
        {
            
var  retval;
            
switch (msg.id)
            {
                
case   10005     // 获取系统时间
                    trace( " msg.details:  " + msg.details);
                    retval 
=   new  Date();
                    
break ;
            }
            
return  retval;
        }
    }
    
else
    {
        application.rejectConnection(clientObj, error);
    }
};

loginOKHandler = function()
{
    
this.onResult = function(res)    //获取onLoginOKCBS的返回值
    
        trace(
"client said: " + res); 
    
    
this.onStatus = function(info)
    
        trace(
"Failed with code: " + info.code); 
    
};

 

 


客户端main.mxml部分关键代码:
private function init():void
            {
                
this.nc = new NetConnection();
                
this.nc.client = this       //指示应对其调用回调方法的对象

                this.nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
            }
            
            private 
function onNetStatus(event:NetStatusEvent):void

            {
                trace(event.info.code);
                
switch(event.info.code)
                {
                    
case "NetConnection.Connect.Success"
:
                        
this.currentState = "LoginOKState"
;
                        
break
;
                    
                    
case "NetConnection.Connect.Failed"
:
                        
break
;
                        
                    
case "NetConnection.Connect.Rejected"
:
                        
var rejreason:String =
 event.info.application.details;
                        Alert.show(rejreason, 
"ERROR"
);
                        
break
;
                        
                    
case "NetConnection.Connect.Closed"
:
                        
this.currentState = null
;
                        
break
;
                }
                       
            
            public 
function close():void
{}
            
            
//登入成功后由服务器端调用, obj为服务器端返回的欢迎信息

            public function onLoginOKCBS(obj:Object):String
            {
                
this.lblSrvNotice.text =
 obj.toString();
                
return "just want to begin"
;
            }
            
            private 
function onBtnGetSrvTimeClick():void

            {
                
this.responder = new Responder(onResult, onStatus);
                
var msg:Object = {"id":10005"details":"GET_SRV_TIME"
};
                
this.nc.call("getInfoFromSrvCBC"this
.responder, msg);
            }
            
            private 
function onResult(obj:Object):void

            {
                
this.tbSrvTime.text = obj.toString();
            }
            
            private 
function onStatus(obj:Object):void

            {
                
var errcode:String = obj.code.toString();
                Alert.show(
"error code:" + errcode, "ERROR"
);
            }
            
            private 
function onBtnLoginClick():void

            {
                
var username:String = this.tbUserName.text;
                
var userpwd:String = this
.tbUserPwd.text;
                
var userinfo:Object = {"username":username, "userpwd"
:userpwd};
                
this.nc.connect(this
.srvurl, userinfo);
            }
 
工程文件下载(包含客户端和服务器端完整代码):http://files.cnblogs.com/riafans/LoginDemo.zip
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值