使用AMFPHP的方便可靠的方式

使用AMFPHP能够极大简化服务端的逻辑。现在我打算简化它的用法。没有应答器,没有侦听器或客户端。 只有一个call(…)和一个回调函数:

  1. private function init():void
  2. {
  3. new Gateway(“http://localhost/amfphp/gateway.php”);
  4. }
  5. private function listButton_clickHandler(event:MouseEvent):void
  6. {
  7. listButton.enabled = false;
  8. call(“Persons.listPersons”, 10, listPersonsCallback);
  9. }
  10. private function listPersonsCallback(object:Object, errorText:String):void
  11. {
  12. listButton.enabled = true;
  13. listButton.errorString = errorText;
  14. if (errorText)
  15. return;
  16. // handle persons from object
  17. }

复制代码

这儿是Persons.php 服务:

  1. < ?php
  2. <?php
  3. class Persons
  4. {
  5. var $link;
  6. public function __construct()
  7. {
  8. $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
  9. if (!$link)
  10. throw new Exception(“DB connection error” . (PRODUCTION_SERVER|> ? “” : “: ” . mysql_error()));
  11. if (!mysql_select_db(DB_NAME))
  12. throw new Exception(“DB selection error” . (PRODUCTION_SERVER|> ? “” : “: ” . mysql_error()));
  13. }
  14. function listPersons($limit)
  15. {
  16. $query = sprintf(“SELECT * FROM persons LIMIT %d”,
  17. mysql_real_escape_string($limit));
  18. $this->_handleResultError($result = mysql_query($query)));
  19. return $result;
  20. }
  21. }

复制代码

下载Gateway.as 和call.as(请到原帖下载。)
根据应用的逻辑不同,回调函数可以有下列的几种:
function():void,
function(errorText:String):void,
function(object:Object, errorText:String):void.

 

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: