flash-php的RPC方案amfphp
时间:2009-03-04 17:13:18 来源:http://ilovelate.blog.163.com/blog/static/601420091212142713 作者:
flash里自己有个二进制的数据传输协议Amf, 幸好php里有个amfphp,那就很方便了, 协议会自动转换php和flash的数据类型,本来都是动态语言,啥都好办。
网址:http://www.amfphp.org/ 官方的,目前最新版本1.9 beta2
下载地址: http://sourceforge.net/project/showfiles.php?group_id=72483
另外要安装个可以提升性能的东东:http://www.teslacore.it/wiki/index.php?title=AMFEXT 目前版本0.8.7
将amfphp放到php项目目录下就可以使用了。
注意将Remote Service php 放到 amfphp\services 目录里面就可以使用了,很简单。
贴个session的例子:
flash端:
php端:
<?php
// Wade Arnold: 1/6/2008
// Example is designed to show how to use PHP sessions.
class Counter {
public function __construct() {
// Check if the session is available or create it.
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
}
}
// Used to increment the session variable count.
public function increment() {
$_SESSION['count']++;
return $_SESSION['count'];
}
// used to destroy the session variable and start over.
public function unregister() {
unset($_SESSION['count']);
return true;
}
// remove the entire session from the server.
public function destroy() {
session_destroy();
return true;
}
}
?>
YY两句,看到热血三国的通讯方式竟然是amfphp的RPC通讯方式的, 这样的话编程模型跟普通的web项目开发就一模一样了, 现在只是说使用了flash界面而已,开发难度大幅度降低。
想想也是, 策略类的游戏,没必要使用客户端游戏的那种socket编程模型,开发起来复杂多了。 可能游戏玩法不一样引起的。如果是有实时战斗的玩法,那必须得socket编程了吧。
时间:2009-03-04 17:13:18 来源:http://ilovelate.blog.163.com/blog/static/601420091212142713 作者:
flash里自己有个二进制的数据传输协议Amf, 幸好php里有个amfphp,那就很方便了, 协议会自动转换php和flash的数据类型,本来都是动态语言,啥都好办。
网址:http://www.amfphp.org/ 官方的,目前最新版本1.9 beta2
下载地址: http://sourceforge.net/project/showfiles.php?group_id=72483
另外要安装个可以提升性能的东东:http://www.teslacore.it/wiki/index.php?title=AMFEXT 目前版本0.8.7
将amfphp放到php项目目录下就可以使用了。
注意将Remote Service php 放到 amfphp\services 目录里面就可以使用了,很简单。
贴个session的例子:
flash端:
// Wade Arnold: 1/6/2008
// Example is designed to show how to use PHP sessions.
package {
// required for flash file and output display
import flash.display.MovieClip;
import fl.events.*;
import flash.events.*;
// required to send/recieve data over AMF
import flash.net.NetConnection;
import flash.net.Responder;
// Flash CS3 Document Class.
public class Counter extends MovieClip {
private var gateway:String = "http://localhost/server/amfphp/gateway.php";
private var connection:NetConnection;
private var responder:Responder;
public function Counter() {
trace("AMFPHP Session Counter Example");
// Event listner for buttons
increment_btn.addEventListener(MouseEvent.CLICK, incrementCounter);
reset_btn.addEventListener(MouseEvent.CLICK, resetCounter);
destroy_btn.addEventListener(MouseEvent.CLICK, destroySession);
// Responder to handle data returned from AMFPHP.
responder = new Responder(onResult, onFault);
connection = new NetConnection;
// Gateway.php url for NetConnection
connection.connect(gateway);
}
// Method run when the "Increment Counter" button is clicked.
public function incrementCounter(e:MouseEvent):void {
// Send the data to the remote server.
connection.call("Counter.increment", responder);
}
// Method run when the "Rest Counter" button is clicked.
public function resetCounter(e:MouseEvent):void {
// Send the data to the remote server.
connection.call("Counter.unregister", responder);
}
// Method run when the "Destroy Session" button is clicked.
public function destroySession(e:MouseEvent):void {
// Send the data to the remote server.
connection.call("Counter.destroy", responder);
}
// Handle a successful AMF call. This method is defined by the responder.
private function onResult(result:Object):void {
response_txt.text = String(result);
}
// Handle an unsuccessfull AMF call. This is method is dedined by the responder.
private function onFault(fault:Object):void {
response_txt.text = String(fault.description);
}
}
}
// Example is designed to show how to use PHP sessions.
package {
// required for flash file and output display
import flash.display.MovieClip;
import fl.events.*;
import flash.events.*;
// required to send/recieve data over AMF
import flash.net.NetConnection;
import flash.net.Responder;
// Flash CS3 Document Class.
public class Counter extends MovieClip {
private var gateway:String = "http://localhost/server/amfphp/gateway.php";
private var connection:NetConnection;
private var responder:Responder;
public function Counter() {
trace("AMFPHP Session Counter Example");
// Event listner for buttons
increment_btn.addEventListener(MouseEvent.CLICK, incrementCounter);
reset_btn.addEventListener(MouseEvent.CLICK, resetCounter);
destroy_btn.addEventListener(MouseEvent.CLICK, destroySession);
// Responder to handle data returned from AMFPHP.
responder = new Responder(onResult, onFault);
connection = new NetConnection;
// Gateway.php url for NetConnection
connection.connect(gateway);
}
// Method run when the "Increment Counter" button is clicked.
public function incrementCounter(e:MouseEvent):void {
// Send the data to the remote server.
connection.call("Counter.increment", responder);
}
// Method run when the "Rest Counter" button is clicked.
public function resetCounter(e:MouseEvent):void {
// Send the data to the remote server.
connection.call("Counter.unregister", responder);
}
// Method run when the "Destroy Session" button is clicked.
public function destroySession(e:MouseEvent):void {
// Send the data to the remote server.
connection.call("Counter.destroy", responder);
}
// Handle a successful AMF call. This method is defined by the responder.
private function onResult(result:Object):void {
response_txt.text = String(result);
}
// Handle an unsuccessfull AMF call. This is method is dedined by the responder.
private function onFault(fault:Object):void {
response_txt.text = String(fault.description);
}
}
}
php端:
<?php
// Wade Arnold: 1/6/2008
// Example is designed to show how to use PHP sessions.
class Counter {
public function __construct() {
// Check if the session is available or create it.
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
}
}
// Used to increment the session variable count.
public function increment() {
$_SESSION['count']++;
return $_SESSION['count'];
}
// used to destroy the session variable and start over.
public function unregister() {
unset($_SESSION['count']);
return true;
}
// remove the entire session from the server.
public function destroy() {
session_destroy();
return true;
}
}
?>
YY两句,看到热血三国的通讯方式竟然是amfphp的RPC通讯方式的, 这样的话编程模型跟普通的web项目开发就一模一样了, 现在只是说使用了flash界面而已,开发难度大幅度降低。
想想也是, 策略类的游戏,没必要使用客户端游戏的那种socket编程模型,开发起来复杂多了。 可能游戏玩法不一样引起的。如果是有实时战斗的玩法,那必须得socket编程了吧。
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/2401/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表