流程如下:
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);
}
};
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 " I 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);
}
{
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 " I 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