【问题标题】:AMFPHP: Serializing Flash Objects over HTTP without the gatewayAMFPHP:在没有网关的情况下通过 HTTP 序列化 Flash 对象
【发布时间】:2010-01-17 15:12:25
【问题描述】:

Flash + AMFPHP 是一个很好的组合。但是在某些情况下,由于各种原因,使用 NetConnection 进行 Flash Remoting 不是正确的工具。 Rob 前段时间有一篇很棒的帖子:http://www.roboncode.com/articles/144

他还有一个很好的例子,说明如何使用 Zend_AMF 将 AMF 传递给 http 请求,无需 POST 和 AMF-request 包来调用 NetConnection 发送的函数。

// Include the Zend Loader
include_once 'Zend/Loader.php';
// Tell the Zend Loader to autoload any classes we need
// from the Zend Framework AMF package
Zend_Loader::registerAutoload();

// Create a simple data structure
$data = array('message' => 'Hello, world!');
// Create an instance of an AMF Output Stream
$out = new Zend_Amf_Parse_OutputStream();
// We will serialize our content into AMF3 for this example
// You could alternatively serialize it as AMF0 for legacy
// Flash applications.
$s = new Zend_Amf_Parse_Amf3_Serializer($out);
$s->writeObject($data);

// Return the content (we have found the newline is needed
// in order to process the data correctly on the client side)
echo "\n" . $out->getStream();

我真的很喜欢这种方法,并且很乐意用 AMFPHP 复制它。为什么是 AMFPHP,你问? “最新”版本使用 amf-ext(一个 C PHP 扩展)来序列化和反序列化数据。它比 ZendAMF 仍在使用的 php 方式快得多。

当然,我已经玩过 AMFPHP 并尝试构建必要的对象并使用 Serializer 类。我什至得到了一个有效的 AMF 字符串,但真正的数据总是被一个“方法包”包裹起来,告诉接收者这是对“Service.method”调用的回答。

那么有没有办法在 AMFPHP 中直接序列化 Flash 对象,而无需网关和方法包装器?

谢谢。

【问题讨论】:

    标签: php flash amf amfphp zend-amf


    【解决方案1】:

    好的,它现在可以工作了。

    它比 Zend_AMF 解决方案稍微复杂一点,但要快得多。这是我的代码:

    $data = array('message' => 'Hello, world!');
    
    // Create the gateway and configure it
    $amf = new Gateway();
    Amf_Server::$encoding = 'amf3';
    Amf_Server::$disableDebug = true;
    
    // Construct a body
    $body = new MessageBody("...", "/1", array());
    $body->setResults($data);
    $body->responseURI = $body->responseIndex . "...";
    
    // Create the object and add the body
    $out = new AMFObject();
    $out->addBody($body);
    
    // Get a serializer and use it
    $serializer = new AMFSimpleSerializer();
    $result = $serializer->serialize($out);
    

    如你所见,我创建了一个新类 AMFSimpleSerializer

    class AMFSimpleSerializer extends AMFSerializer
    {
        function serialize(&$amfout)
        {
            $encodeCallback = array(&$this,"encodeCallback");
    
            $body = &$amfout->getBodyAt(0);
    
            $this->outBuffer = "";
            $this->outBuffer .= amf_encode($body->getResults(), $this->encodeFlags, $encodeCallback);
            $this->outBuffer = substr($this->outBuffer, 1);
    
            return $this->outBuffer;
        }
    }
    

    这个类只有在安装了 amfext 的情况下才有效,但可以很容易地修改为使用 php enocding 过程。我没有实现它,因为我在 AMFPHP 的大量修改版本上构建了它。

    我希望我用真正的 AMFPHP 对应物替换了我的代码中的所有类。明天我将尝试对此进行测试,并在必要时更新此答案。

    完成后,我意识到现在几乎没有任何来自 AMFPHP 的内容实际上留在类中,它只是调用 amf_encode 并删除第一个字节,以便客户端可以理解他得到了什么。

    简单、简单、快速。

    【讨论】:

      【解决方案2】:

      这是一个简化版,不需要amfext:

      require_once( 'amfphp/core/amf/app/Gateway.php');
      require_once( AMFPHP_BASE . 'amf/io/AMFSerializer.php');
      
      $data = array('message' => 'Hello, world!')
      
      $serializer = new AMFSerializer();
      $serializer->writeAmf3Data( $data );
      
      print $serializer->outBuffer;
      

      不需要换行符和子字符串。 AMFPHP 1.9,Flex 3.4。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-14
        • 1970-01-01
        • 2018-08-26
        • 1970-01-01
        • 2015-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多