我写的网页游戏框架openjpfgame AMF-PHP通讯部分

     最近工作游戏项目比较忙,目前openjpfgame 断断续续开发当中,各个部分之间通讯Flex<--->Java,Flex<--->Php,Php<--->Java都已经开发好。初步确定做一MMORPG游戏Demo版本。

     目前Flex<--->Php通讯方式采用Flash自带的RemoteObject,而经过多次试验使用cairngorm和PureMVC框架对比,我决定Flex本身采用PureMVC框架。PHP本身采用ZendFrame框架,原因很简单,zend出身豪门,强大全面,不愧是Php框架中的战斗机。我把部分Flex以及PHP源码贴出来:

图片看不清楚点击放大
我写的网页游戏框架openjpfgame AMF-PHP通讯部分_第1张图片
 

 

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:

 

 

图片看不清楚点击放大
我写的网页游戏框架openjpfgame AMF-PHP通讯部分_第2张图片
 

 

index.php

/**
  *
  * @author luodongfu
  *
  */ 

 

@header('Content-type:text/html;charset=UTF-8');
 error_reporting(E_ALL|E_STRICT);
 date_default_timezone_set('Asia/Shanghai');
 define('ROOT_DIR', dirname(dirname(__FILE__)));
 define('WEB_ROOT', 'http://127.0.0.1/openjpfgame/');
 define('CACHE_DIR', 'C:/cache');
    define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
    define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
 define('GAME_SERVICE_PATH', APPLICATION_PATH . './application/game');
    set_include_path('.'.PATH_SEPARATOR.'./library'.PATH_SEPARATOR.'./application/models/'.PATH_SEPARATOR.'./application/game/'.PATH_SEPARATOR.get_include_path());

 

require('Zend/Loader/Autoloader.php');
    Zend_Loader_Autoloader::getInstance()->registerNamespace('Zend_');

$config=new Zend_Config_Ini('./application/config/config.ini',null, true);
 Zend_Registry::set('config',$config);
 $dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
.........................................

...........................................

 

 

/**
  *
  * @author luodongfu
  *
  */ 

GatewayController .php

//所有游戏请求都在这里处理

class GatewayController extends Zend_Controller_Action
{
    public function init()
    {
  $this->getHelper('ViewRenderer')->setNoRender();
       
    }
    public function serviceAction()
    {
     echo Zend_Amf_Server_Factory::getInstance()->handle();
    }
}

 

 

 

 

你可能感兴趣的:(游戏,PHP,框架,Zend,网页游戏)