package org.openjfgame.service
{
import flash.net.NetConnection;
import flash.net.Responder;
import org.openjfgame.event.*;
import org.openjfgame.utils.*;
import flash.events.*;
import org.openjfgame.msg.local.amf.BaseAmfMsg;
import org.openjfgame.msg.remote.amf.AmfMessage;
/**
*
* @author luodongfu
*
*/
public class GameNetConnection extends NetConnection
{ //php 游戏gameway 对应php:define('WEB_ROOT', 'http://127.0.0.1/openjpfgame/gateway/index');
private var gateway:String; //用户ID
private var userID:int; //会话ID
private var sessionID:String;
private var _netName:String;
public function GameNetConnection( netName:String,gateway:String)
{
super();
this.gateway=gateway;
this._netName=netName; //给连接NetConnection监听链接状态并处理
this.addEventListeneradminNC.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
//开始连接PHP
this.connect(this.gateway);
}
private function statusHandler(evt:NetStatusEvent):void{
trace(evt.info.code); //根据PHP结果做相应处理
switch(evt.info.code){
case "NetConnection.Connect.Rejected":
var appmsg:String = (evt.info.application == undefined) ? "" : evt.info.application; //使用PureMVC通知
Globals.sendNotification(NotificationConst.CONNECT_AMF_SERVER_REJECTED,appmsg);
break; //使用PureMVC通知
case "NetConnection.Connect.Failed":
Globals.sendNotification(NotificationConst.CONNECT_AMF_SERVER_FAIL,evt.info.code);
break; //使用PureMVC通知
case "NetConnection.Connect.Closed":
Globals.sendNotification(NotificationConst.CONNECT_AMF_SERVER_CLOSED,evt.info.code);
break; //使用PureMVC通知
case "NetConnection.Connect.Success":
Globals.sendNotification(NotificationConst.CONNECT_AMF_SERVER_SUCCESS,evt.info.code);
break;
}
}
//成功接收PHP返回来的信息并通知
private function onResult(result:Object):void
{
var amfMessage:AmfMessage=new AmfMessage();
amfMessage.copyValue(result);
switch(amfMessage.result)
{
case Constants.REMOTE_SERVICE_RESULT_FAIL:
case Constants.REMOTE_SERVICE_RESULT_SUCCESS:
this.dispatchEvent(new JfEvent(amfMessage.remoteMethod,amfMessage.data));
break;
default:
}
} //失败发出通知
private function onFault(result:Object):void
{
Globals.sendNotification(NotificationConst.REMOTE_EXCEPTION,result.toString());
}
//每次发送信息给PHP必须调用这个函数
public function sendRemoteRequest(remoteMethod:String,bodyMessage:Object):void{
var remoteMsg:BaseAmfMsg=new BaseAmfMsg(remoteMethod,bodyMessage);
if(this.userID>0&&this.sessionID.length>0)
{
remoteMsg.userID=this.userID;
remoteMsg.sessionID=this.sessionID;
}
this.call(remoteMethod, new Responder(onResult, onFault), remoteMsg);
}
private function resetUserInfo():void{
this.userID=-1;
this.sessionID=null;
}
public function setUserInfo( userID:int,sessionID:String):void{
if(userID>0&&sessionID.length>0)
{
this.userID=userID;
this.sessionID=sessionID;
}
}
public function get netName():String
{
return this._netName;
}
}
}
/**
*
* @author luodongfu
*
*/
package org.openjfgame.msg.remote.amf
{
import org.openjfgame.msg.BaseMsg;
public class AmfMessage extends BaseMsg
{
public var result:int;
public var exception:String;
public var data:*;
public var remoteMethod:String;
public function AmfMessage()
{
super();
}
}
}
PHP zend: