vini123

博客园 首页 新随笔 联系 订阅 管理

看标题就可以猜到,本实践使用了Flash Remoting方式连接服务器,并采用最新的AMF3数据传输协议,好处就不多说了。

首先要解决的问题就是怎样把CakePHP和AMFPHP结合,首先找到的是cakeamfphp,看一下最后版本更新日志,竟然是06年10月,接着找到作者为cakephp 1.2写的修改方法,显然他不想再更新cakeamfphp了。

继续寻觅,又找到了cakeswxphp,支持cakephp1.2,看起来不错,正打算用的时候发现作者做了个性能测试,cakeswxphp性能竟然低的可怕,先放着,cakephp果然没有让我失望,在强大的google帮助下,又找到了一个叫做cpamf的插件,并且是参考了cakeamfphp和cakeamf(后来才发现,cakeamfphp的作者又重新写了一个新的cakephp插件,不过需要AMFEXT的支持)写的,并且可以不用AMFEXT,最方便之处是可以让flash以remoting方式直接访问cakephp里controller的action来传递和获取数据,这就是我想要的了。

搭建cakephp环境,把下载回来的CPamf解压到 app/plugins 目录下,然后直接访问 http://localhost/cpamf/gateway ,显示如下信息,表示安装成功。

amfphp and this gateway are installed correctly. You may now connect to this gateway from Flash.

Note: If you’re reading an old tutorial, it will tell you that you should see a download window instead of this message. This confused people so this is the new behaviour starting from amfphp 1.2.

View the amfphp documentation

Load the service browser

建立数据表:

CREATE TABLE IF NOT EXISTS `games` (
  `gameId` int(11) NOT NULL auto_increment COMMENT 'game id',
  `gameName` varchar(100) collate utf8_unicode_ci NOT NULL COMMENT 'game name',
  `gameDeveloper` varchar(100) collate utf8_unicode_ci NOT NULL default 'Anonymous' COMMENT 'game Developer',
  PRIMARY KEY  (`gameId`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC AUTO_INCREMENT=100000 ;

写Model和Controller:

Game Model (Game.php):

class Game extends AppModel
{
	var $name = 'Game';
	var $primaryKey = 'gameId';
}

Game Controller (games_controller.php):

class GamesController extends AppController
{
	var $name = 'Games';

	function index() {}

	function getAllGames()
	{
		$games = $this->Game->find('all');
		//$gameCollection = new ArrayCollection($games);
		return $games;
	}

	function getGame($id = 0)
	{
		$game = $this->Game->find('all', array('conditions' => array('Game.gameId' => $id)));
		return $game;
	}
}

查看service browser:
打开 http://localhost/cpamf/browser ,顺利的话,就会在左边栏显示出GamesController,点击后就会在右栏显示出Controller里的action列表,看看里面是不是有getAllGames和getGame,选择getAllGames,点击Call按钮,就会在下方Results标签内看到获取的数据了,如图:

2009-04-10_235915

用Flash传递和获取数据:
这里我用的flex builder 3来建立actionscript project来测试,代码如下:

package {
	import flash.display.Sprite;
	import flash.net.*;

	public class UseBlobStat extends Sprite
	{
		private var _netConn:NetConnection = new NetConnection();

		public function UseBlobStat()
		{
			_netConn.objectEncoding = ObjectEncoding.AMF3;
			_netConn.connect("http://localhost/cpamf/gateway");
			// 获取全部游戏
			_netConn.call("GamesController.getAllGames", new Responder(GetGamesComplete, GetGamesError));
			// 传递id参数来获到指定游戏
			_netConn.call("GamesController.getGame", new Responder(GetGameComplete, GetGamesError), 100000);
		}

		private function GetGamesComplete(e:Array):void
		{
			for ( var i:int = 0; i < e.length; i++)
			{
				trace(e[i]['Game']['gameId'] + '\t' + e[i]['Game']['gameName'] + '\t' + e[i]['Game']['gameDeveloper']);
			}
		}

		private function GetGameComplete(e:Array):void
		{
			trace("---------------传递参数获取------------------");
			trace(e[0]['Game']['gameId'] + '\t' + e[0]['Game']['gameName'] + '\t' + e[0]['Game']['gameDeveloper']);
		}

		private function GetGamesError():void
		{
			trace("失败了...");
		}

	}
}


解决amfphp 1.9中文乱码:
首先是query之前公认的mysql_query(”set names ‘utf8′”);
接着修改gateway.php的127行:
$gateway->setCharsetHandler(”mbstring”, “UTF-8″, “UTF-8″);


文章来自:http://junnan.org/blog/523

posted on 2012-10-16 16:32  黑夜丶残枫  阅读(268)  评论(0)    收藏  举报