AMFphp 的简介

作者: 嘎嘎 2009-11-16,01:48 下午 周一

  AMFPHP实际上是一个提供了FLEX等类型的客户端和PHP服务器端程序的一个开源的,免费通讯机制,它可以使得FLEX等客户端和服务器的PHP程序之间基于数据结果的交换。flash里自己有个二进制的数据传输协议Amf, 幸好php里有个amfphp,那就很方便了, 协议会自动转换php和flash的数据类型,本来都是动态语言,啥都好办。

  那为什么需要AMFPHP呢?

  事实上FLEX本身就提供和和服务器端交互的各种手段,比如HTTPSERVICE组件本来就可以轻易调用基于 PHP或者其他语言的服务器端程序来获得结果,但是很显然它是基于HTTP的文本协议,对于大量数据的交换显然不是非常高效,所以AMFPHP在设计的时候就考虑到这个问题,它通过使用二进制协议来提高通讯的效率,从而提高了在传输大量数据的时候的效率。

  还有一个优点就是,AMFPHP会自动处理PHP端的数据类型到FLEX客户端的数据类型的匹配,从而降低了FLEX使用PHP服务端程序的成本。

  那么,一般如何使用AMFPHP呢?

  最普通的用法是一般把AMFPHP部署在服务器端,它可以去数据库里去取得各种数据,然后返回给FLEX的DataGrid组件来展示,而对于FLEX客户端,在正确配置后只需要像调用FLEX本地方法一样来取得PHP服务器端程序返回的数据!

AMFPHP的官网:http://www.amfphp.org/

另外要安装个可以提升性能的东东:http://www.teslacore.it/wiki/index.php?title=AMFEXT
将amfphp放到php项目目录下就可以使用了。
注意将Remote Service php 放到 amfphp\services 目录里面就可以使用了,很简单。

贴个session的例子:
flash端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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);
        }
    }
}

php端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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编程了吧。

1. 英文相关文章

2. 中文相关文章

暂无相关文章

分类 : FLASH,PHP (阅览:1,002 次) Tags :

评论

还没有任何评论。


留下评论


|:victory:| |:tongue:| |:titter:| |:time:| |:sweat:| |:smile:| |:shy:| |:shocked:| |:sad:| |:mad:| |:lol:| |:kiss:| |:hug:| |:huffy:| |:handshake:| |:cry:| |:call:| |:biggrin:|