存档

文章标签 ‘Remoting’

(转)Using Flash Remoting from JavaScript (AIR)

2007年10月14日 1 条评论

Flash Remoting. The product now more commonly referred to simply as “remoting” is woven deep into the very fabric of many of Adobe’s technologies. It’s available in ColdFusion and LiveCycle Data Services on the server. Of course you know you can use it from Flash and therefore Flex on the client. Did you know however, that this also extends to JavaScript in AIR?

Understanding Flash Remoting

To level-set, Flash Remoting is a technology introduced as a product offering circa Flash Player 6. The idea was that XML, and by extension SOAP, were too heavy. They required too much memory on the client, and too much bandwidth going across the wire. This was especially noticeable as the number of records increased. Using XML also generally meant additional development time as existing logic was wrapped to be exposed as a service.

By contrast, Flash Remoting alleviates memory requirements by using a binary serialization of the data structures coming across the wire. This also generally means faster transmission of the data in both directions (to and from the server). The protocol is still HTTP, but the payload is binary. The other benefit of Flash Remoting is that via a server proxy (which is why the product existed), developers could talk directly to existing server logic.

While you’ll find Flash Remoting in a variety of Adobe products, you’ll also find various alternative implementations including both open source and commercial offerings.

Using Flash Remoting

So how does a Flash-based technology get leveraged from JavaScript? Well, it’s important to remember that the HTML control in AIR is integrated with Flash. Via a feature commonly referred to as “script bridging” it is possible to use Flash API’s directly from JavaScript. Since Flash Remoting is part of the core Flash Player functionality on the client, it can also be used directly from JavaScript.

Using remoting from JavaScript is actually really straightforward. The first thing you need to do is specify where on your server the remoting gateway exists. This can vary slightly depending on the implementation you’re using. I use ColdFusion 8 on the server, so my URL is “http://www.mydomain.com/flashservices/gateway”. Once you’ve got that piece of information you use the Flash NetConnection class to establish a connection to the server.

var GATEWAY = 'http://www.mydomain.com/flashservices/gateway'; 
var conn = null; var response = null; 
function doLoad() { 
document.getElementById( 'btnHello' ).addEventListener( 'click', doHello ); 
conn = new air.NetConnection(); conn.connect( GATEWAY ); }

Before you start sending data to the server, you’ll need a means to handle the response, which could be a “result” or a “status” (fault). You’ll want to write a JavaScript function for each case. In the case of a fault, you’ll want to look at the “description” property of the event passed to the function to learn more about what went wrong. What you’ll get in the case of a

“result” will vary depending on the implementation and the type of data being returned.

function doFault( e ) { alert( e.description ); } 
function doResult( e ) { alert( e ); }

Calls to the server happen asynchronously, so you’ll need a way to tie the response events to the functions you just created. This happens via a Responder object instance. When creating a Responder, you pass the functions that you setup to use for the various response types (result and fault). Then the only thing left to do is to actually make the call. This happens via the NetConnection.call() method.

function doHello() { 
var name = document.getElementById( 'txtName' ).value; 
response = new air.Responder( doResult, doFault ); 
conn.call( 'work.Greeting.hello', response, name ); }

Notice the first parameter to the NetConnection.call() method. This is a string that refers to the object you’re using on the server. If the technology you’re using uses packaging, then you’ll need to specify that as well. For ColdFusion, the package is the directory structure to the ColdFusion Component (CFC). After the package is the class you’ll be invoking. Finally, the last part is the name of the method on the class.

In this case, I’m using a simple string for data. More realistically you’ll want to send an object or array of objects. The specifics for doing this will vary slightly depending on the implementation of Flash Remoting that you’re using, but is generally exactly what you’d expect: an Array, an Object, or an Array of Objects (i.e. [{id: 1, name: ‘Adobe’}, {id: 2, name: ‘Flash Remoting’}]).

There’s a lot of documentation both from Adobe and the community on the deeper uses of Flash Remoting, and I encourage you to continue exploring.

分类: 未分类 标签:

(转)AMFPHP1.9&PHP 5.22 bug

2007年10月9日 评论已被关闭

最近烦心的事情还真是多,当属最大的就是amfphp1.9与php5.2.2不兼容的问题,我曾经静下心来寻找amfphp中的bug,结果还是没有找到,一开始只是怀疑HTTP_RAW_POST_DATA这个变量,修改php.ini后仍旧没有解决解决问题,只好乖乖地用回php 5.2.1但是php5.2.1的GD库存在一个很大的bug不能处理尺寸过大的图片。
今天一早上 5 1/2 blog (amfphp开发者:patrick)看到他已经对这个bug作出回应,并且给出了解决方案。原来正是我怀疑的东西,HTTP_RAW_POST_DATA 在PHP中不再支持(被列为过时)需要使用file_get_contents(‘php://input’)来替代,所以曾经amfphp获取Flash数据时失败(因为amfphp中是利用这种方法来获取传入的数据的)。必须要添加下面代码:
if(!isset($GLOBALS['HTTP_RAW_POST_DATA'])){
    $GLOBALS['HTTP_RAW_POST_DATA']=file_get_contents(‘php://input’);
}
(该代码添加到core/amf/app/Gateway.php 140行以前的地方就可以了)

 

分类: 未分类 标签:

建立Remoting方法和类

2007年9月23日 评论已被关闭

当被请求时,AMFPHP使用一个简单的配置来加载适当的服务。服务中的名字的"."被映射为"/",然后它附加一个.php扩展和设法查找services文件夹中的文件。例如:

this.service = new Service(this.gatewayUrl,
  null, "com.example.MyService", null, null);

当网关被调用,它将会尝试在{servicefolder}/com/example文件夹中查找MyService.php文件。网关期待在和service相同的文件中有定义一个类。在这个例子中,php文件必须包含一个类叫MyService,当Windows不忽略大小写时,你的服务器配置也一样,所以保证两者相同。

The methodTable

在你的service类构造函数中,你需要告诉amfphp哪个方法输出到Flash,通过定义一个成员变量,methodTable,下面是一个典型的methodTable:

<?php class MyClass{
  function MyClass() { 

$this->methodTable = array( 

"doStuff" => array( 

"description" => "Puts the value into a with name key", 

"access" => "remote ), 

"doAnotherThing" => array( 

"description" => "Retrieves the value of the se variable", 

"access" => "remote" ) ); 

} 
[方法:doStuff and doAnotherThing 在这里定义]
  } ?>

唯一必须的是 access 关键字,设置它为remote,意思就是让这个类可以用到 Remoting。methodTable也可以绑定许多的标记信息,包含:arguments,return types,recordsets的pagesize,类映射等等。标记信息可以被AMFPHP的编码发生器使用,用来建立stub as,

The MethodTable class

如果在你的类中有更多的方法,methodTable会被得体积大,你可能会想要把它放在分离的文件中,从构造函数中使用 include 它。methodTable也可以由methodTable类产生,使用你的JavaDoc注释来产生methodTable。为了使用methodTable类,在你的类中一开始就包含它进来:

include(AMFPHP_BASE . "util/MethodTable.php");

然后在你的构造函数中写入:

$this->methodTable = MethodTable::create(__FILE__);

然后你需要在你的方法顶部写入 javadoc 注释,以下的标签都支持:

  • @desc
  • @access (set to remote to export a class)
  • @roles (comma-separated)
  • @instance
  • @returns
  • @pagesize

Using the MethodTable class during development is recommended as having to modify the methodTable all the time is a major pain and a waste of time. You should hardcode the methodTable before deployement for performance. You can do this by browsing to your method using the HTML service browser and clicking on the ‘MethodTable’ link next to the class name.


分类: 未分类 标签:

在Flash中安装Remoting组件

2007年9月23日 评论已被关闭
What you need

To use amfphp, you need to have the following:

  • Flash MX 2004 or Flash 8 (not free)
  • Amfphp zip file (free)
  • ActionScript Remoting components (free)

You don’t need to buy anything else outside of Flash to make amfphp run. In particular, just so we’re clear, you don’t need to shell out 1000$ for Macromedia’s version of Remoting (amfphp is a drop-in replacement for that). Before you ask, yes, Macromedia doesn’t mind us giving away amfphp; in fact there is an article by none other than Robert M. Hall on Devnet that focuses on amfphp.

Installing Remoting components

If you’ve never developed for Flash Remoting, you’ll need to install it as an extension to Flash 8 or MX 2004. This manual assumes that you are familiar and comfortable with Flash 8 or MX 2004 and Actionscript. You can get a 30-day trial for Flash at Macromedia and read some good tutorials on Actionscript at actionscript.org. If you have already developed using Flash Remoting, you can safely skip this section and go to Installing amfphp.

Installing the Actionscript remoting components

To get access to the Remoting classes, you first need to install an extension to Flash 8 or MX 2004. You can download the AS2 remoting components here. Double-clicking the downloaded file should install the components automatically. You will also want to download the source for the AS2 classes, listed on the same page, if you plan on using FAME to create your movies.

Getting familiar with Remoting

下载的MXP包含五大元素:

  • The NetConnection debugger, a Flash movie that shows the requests to and from the server. You probably want to create a shortcut to the debugger on your desktop or QuickLaunch bar. Under Windows XP, the debugger can be found under :
    C:\Documents and Settings\{Your username}\Local Settings\Application Data\Macromedia\Flash {version}\{language}\configuration\WindowSWF\NetConnection Debugger.swf.
    On OS X, you can find it under:
    /Users/{username}/Library/Application Support/Macromedia/Flash {version}/{language}/Configuration/WindowSWF\NetConnection Debugger.swf
  • The service browser, which allows you to browse your services and see the available methods with their corresponding arguments. The service browser is largely unused with AMFPHP, which comes with its own full-featured HTML-based service browser with features such as Actionscript generation. Nevertheless, AMFPHP is compatible with MM’s service browser, which can be accessed in the Flash IDE from Windows > Other Panels > Service Browser.
  • Help files. Press F1 to access.
  • The Remoting libraries. These are available from Windows > Others > Libraries > Remoting Classes. You will need to include the RemotingClasses and RemotingDebugClasses in the library of each movie you create. If you want to have the RemotingClasses and RemotingDebugClasses additionally available inside the Components panel, you can copy the following file:
    C:\Documents and Settings\{Your username}\Local Settings\Application Data\Macromedia\Flash {version}\{language}\configuration\Libraries\Remoting.fla
    To the configuration\Components\ folder, and reload the Components panel.
  • A connector component available under Components > Data components > RemotingConnector.
分类: 未分类 标签:

amfphp的问题汇总

2007年9月23日 评论已被关闭

下面的问题都是在我在实际操作中碰到的一些问题:

在browser中可以看到类名,点击可就是出错(如图),最后发现问题在php文件的编码问题上。

还有一个问题是出现在类的名称上。哎!!!!!!!
image

 

又来了一个问题,无返回状态:
php中的代码:

function gea(){
return "!!!!";
}

在flash中remoting时用service.gea 无返回成功提示,一定要写成带参数的形式:gea(”),带了一个空字符串,这样就返回成功了

分类: 未分类 标签: ,

Remoting开始教程

2007年9月23日 评论已被关闭

(备注:此教程根据原英文教程"翻译"而来,只供个人学习时使用,翻译的质量有限,能看就凑合着看吧,大概的意思都表现出来了)

Remoting在开始可能有一点的难使用,无论如何,在你使用它一段时间后,它会变为非常的简单自然。

当在flash中开始一个Remoting工程的时候,开始打开windows->common Libraries->Reomoting,然后拖 RemotingClasses和RemotingDebugClasses 到舞台上。第二步是开始 NetConnection debugger 通过在第一帧上插入以下的代码或者你的主类初始函数中:

import mx.remoting.debug.NetDebug;
    import mx.remoting.*; 

import mx.rpc.*; 

NetDebug.initialize();

Remoting依赖在你的每个新帧或者类中使用这组导入的语句,否则当你尝试编译的时候会告诉你不能找到类。

下步是建立服务。Service构造函数有5个参数,在大多数场合下,你将只需要指定前面三个,第一个是网关的地址,第二个通常是null,第三个是service的位置:

var service:Service = new Service
    ('http://localhost/amfphp/gateway.php', null, 

'com.company.MyService');

这将会连结到你的网关和查找 services文件夹中的com/company/MyService.php 文件。这个文件必须有一个类叫 MyService,定义了 一个methodTable成员(详细资料在下面)。

你可以重新使用同样的service 来调用不同的方法。实际中,大多数的人都喜欢放置他们的service在一个类中,作为一个静态的变量或者单独的来访问。

一旦Service被建立,就开始调用远程方法了:

var pc:PendingCall = service.myMethod("arg1", {x:'myX'});
    pc.responder:RelayResponder = 

new RelayResponder(this, "onSuccess", "onFault");

为了调用一个方法,你一开始调用它好像它是一个本地服务一样,你将会接收到一个 pending call。在远程服务器上,myMethod 方法在MyService类中,如果它在methodTable中被定义为一个 remote函数(详细资料在下一页)

然后你使用一个RelayResponder来设置返回,远程方法将会在下一帧自动被调用。如果这个方法工作了,onSuccess会被调用,而onFault不会,onFault不会捕捉超时,忽略网关或者不幸的错误(NetConnection.Call.BadVersion),只有逻辑错误会被触发,程序员通过exceptions(PHP5)或者trigger_error (PHP4,然后E_USER_ERROR 和较少的critical errors 会被报告)。

The Relay class 工作类似RelayResponder,但是使用references代替字符串,意思是:如果你忽略返回的类型,你会获得一个编译错误。

现在你需要设置responders:

function onSuccess(re:ResultEvent) {
  trace("Success!"); 

NetDebug.trace(re.result); } 
function onFault() {
  trace("Fudge! :("); }

成功返回的结果是第一个参数.result。因此可以使用 re.result。这个结果是一个本土的Flash格式(字符,数字,数组,对像等等)。

mx.remoting.RecordSet

到现在为止,你已经看到了怎么样调用远程方法和接收返回来的数据。到如今你知道它是可笑地使用Remoting简单的调用一个远程方法带有复杂参数。无论如何,最引起兴趣地功能是Remoting的结果集操作。

如果你从你的远程方法中发送回来一个结果集(返回:mysql_query("SELECT * FROM myTable");),这个结果将会是一个mx.remoting.RecordSet的实例。这是一个特别的类,它有大量的好处,有用的方法:

  • getItemAt(index) and removeItemAt(index)
  • 通过自定义函数过滤(过滤方法)
  • 通过自定义函数排序 (排序方法)
  • 事件派遣

如果那还不够,MM的 V2组件打算使用Recordsets作为dataProviders 例如:如果你有一个datagrid在舞台,如果你设置它的dataProvider为你的结果,它将会显示 headers和 columns和rows,允许不同的列排序。对单独的一行代码,有许多功能。把它们放在一起:

import mx.remoting.debug.NetDebug;
    import mx.remoting.*; 

import mx.rpc.*; 

NetDebug.initialize(); 

var service:Service = new Service 

('http://localhost/amfphp/gateway.php', null, 

'com.company.MyService'); 

var pc:PendingCall = service.myService("arg1", {x:'myX'}); 

pc.responder:RelayResponder = new RelayResponder(this, 

"onSuccess", "onFault"); 
function onSuccess(re:ResultEvent) {
    var rs:RecordSet = RecordSet(re.result); 

myDatagrid.dataProvider = rs; }
function onFault() {
    trace("Fudge! :("); }

Remoting有许多的其它功能,也同样喜欢鉴定和web service 消费,但是你可以马上看到就像前面的你可以实现许多功能,比LoadVars和XML更自然。


分类: 未分类 标签: ,

AMFPHP 1.2中更新了什么

2007年9月22日 评论已被关闭
(备注:此教程根据原英文教程"翻译"而来,只供个人学习时使用,翻译的质量有限,能看就凑合着看吧,大概的意思都表现出来了)
AMFPHP 1.2有什么新的东西

Amfphp 1.2 建立在amfphp 1.0 和短期的1.1版本基础上,它有以下的新的特性:

增强服务浏览

服务浏览现在可以在客户端上使用模似请求。意思就是:你不需要安装Flash就可以测试出你的服务,它对各种团队使用amfphp非常简单。测试速度也快了,代码产生被提高,支持ARP框架和自动保存。

新的文件夹结构

新的文件夹结构混乱会比较小,导航比较简单,不同版本的amfphp之间移植

提高类的映射

Amfphp now features complete class mapping options from the gateway.

What’s new in AMFPHP 1.0

A lot has changed since AMFPHP’s last version 0.9b. We’ve switched to a better engine, which allows us to add some much-wanted functionality. Here’s an outline of what’s new:

New engine

The core of the new engine was written by Justin Watkins, and was created to be 100% object oriented, secure, and reliable. Each step of the interpretation of AMF messages is carried through two chains: filters and actions. Expanding the capabilities of AMFPHP is as simple of creating a new filter or action, and modifying the chain in the right spot.

Enhanced support for different charsets

Flash uses Unicode, while PHP natively uses latin1 (ISO-8859-1). We’ve solved the babel tower by adding transparent support for charset transcoding using iconv, meaning you can work in your native language and charset, by calling the appropriate method in your gateway.php file.

Faster serialization

AMFPHP is renowned to be fast, and we’ve made it even faster by squeezing every bit of power from PHP in this new release. We’ve tightened the serializer and tweaked it for large resultsets; sending a resultset with 5 fields and 400 rows takes about 80% less time it used to. We’ve also tamed its memory usage, meaning it can be easily deployed on high-traffic servers.

Less NetConnection.Call.BadVersion error

The new loose mode sets up an output buffer to suppress echo, print and other calls to functions that output to the client to protect you from yourself. This should eliminate a good half of the common causes for the dreaded NetConnection.BadVersion error.

远程追踪

我们添加了一个新的NetDebug::输出静态方法就像在AS中的NetDebug.trace功能一样,但是它是远程工作的,它可以用到任何自定义的服务类。

Pageable recordsets

Pageable recordsets allow you to send back SQL resultsets in chunks. On the Flash side of things, Macromedia’s classes handle synchronization between the client and server and the functionality is already plugged into the data grid, meaning it is easy to work with large recordsets.

Class mapping

AMFPHP now allows mapping objects in method arguments to custom PHP classes. To use this feature all you need to do is set the type under the arguments in the method table.

Tighter integration into the service browser panel

The new version is compatible with both Macromedia’s service browser, and Muzak’s new (and better) panel. We’d like to thank Muzak who furnished the technical details on how we could achieve this.

HTML-based service browser

We’ve created our own HTML-based browser that features, in addition to service description, automatic Actionscript generation (scaffolding). That’s right, save time by browsing to your service and copy-pasting the code generated for you in Flash. Both AS2 (using the Service class) and AS1 (using NetServices.as) are generated. With a little bit of JavaDoc you can get it to generate your methodTable as well.

Tighter integration into the NetConnection Debugger

We’ve added support for new events in the NetConnection debugger, which means more verbose debugging output, which should help you figure what is going on between Flash and PHP.

Cookieless sessions

AMFPHP automatically handles sessions for you and sends back a header to Flash with a redirect to the gateway with the PHPSESSID appended to the query, meaning cookieless sessions are supported.

Authentication

Well, not really new, but it works more smoothly now because of the aforementioned cookie less sessions.

PHP5 compatibility

We’ve fixed incompatibility issues with PHP5 caused by its new pass-by-ref mechanism for class instances.

PHP5 exception support

You can now throw your own exceptions inside of your services and they will be sent back to Flash automatically in the onStatus handler.

Soap Client support

The native PHP5 SOAP extension is supported for web services, and it is much faster than NuSOAP and PEAR::SOAP alternatives.

More databases supported

SQLite, MySQLi, Oracle (oci8), Frontbase, Pear::db and PDO added to the roster of AMFPHP-compatible databases.

Transparent client header support

You can now use the Headers::getHeader($key) function anywhere in your services to get a client header by key.

FlashComm and SSL support

New headers are being sent by AMFPHP for compatibility with FlashComm and SSL.

支持新的返回类型

返回的类型可以是:二进制,原始,数组,structof, 和更多的为专门的程序。也添加了一个新的 methodTable 选项

fastArray is implemented for faster array handling for large multi-dimensional arrays.

 

New gateway options for deployment

disableStandalonePlayer(), disableServiceDescription(), disableTrace() and disableDebug() methods available for more secure deployement.

简单的安装和较好的文档

我们相信 AMFPHP是好的,它应被蔓延到全世界,新的文档和简单的安装程序应该可以帮助你从零开始。

A bunch of bug fixes

We’ve searched high and low and corrected FlashComm compatibility issues, authentication issues, problems with corrupted AMF messages and more.

分类: 未分类 标签: ,

Amfphp手册

2007年9月22日 评论已被关闭
(备注:此教程根据原英文教程"翻译"而来,只供个人学习时使用,翻译的质量有限,能看就凑合着看吧,大概的意思都表现出来了)
Amfphp手册

Amfphp是一个php的RPC工具包,它允许无缝的与PHP通讯:

1.flash 和 flex Remoting
2.JavaScript 和 Ajax与JSON
3.XML客户端与XML-PRC

什么是RPC

PRC(远程程序呼叫)是一种客户端与服务器之间数据的通讯。你调用一个方法在一个本地对像与多种多样的参数,设置一个返回,接收一个结果。你不用担心你要怎么要发送和接收数据。实施细则正在走抽象,所以它看起来好像你通话本地方法。

在后台它是怎么工作的

服务器和客户端,暂时称呼 PHP 和 Flash ,对描述调用方法和复杂数据达成了一致协议。客户端串行请求和发送到网关。 Amfphp然后自动:

1.Deserializes请求
2.查找相应的远程类
3.实例化类
4.完成安全检查
5.调用远程方法,使用指定的参数
6.连续返回数据

Amfphp 可以完全地连续和deserialize复杂类型。除对像和数组之外,它还支持从数据库连结的资源,也就是说你可以简单的从你的远程方法中返回 mysql_query和amfphp将会知道该对它做什么。如果平台支持它(通常的,只有Flash Remoting) ,它可以操作循环引用和对像类型。它也支持简单地远程调试。最终,amfphp 与浏览器服务一起发生,允许你测试您服务之前创建一个前端。

分类: 未分类 标签: ,