Best practices: Custom class mapping using OpenAMF and AS3 (Flash CS3 – not Flex)
September 23rd, 2007
OpenAMF is a free open-source alternative to Adobe’s (formely Macromedia’s) Java Flash Remoting. It’s difficult to find any tutorials or posts about OpenAMF using AS3, because it seems that the OpenAMF project has stopped since 2006.
Anyway, at my current project we are using OpenAMF with AS3 (Flash CS3) and it works like a charm. But there are some important differences between AS2 and AS3 Flash Remoting using Flash CS3. To avoid running into any issues – particulary mapping custom classes – follow the following tips. But first of all thanks to Marc Schachtel for his great support on server-side
.
Best practices
-
Create on the server- and Flash-side identical simple objects using the Value Object pattern to send and receive custom objects over the wire. Assuming a
UserVOwould be created on server-side as follows:UserVO.java ( Download code )
-
package de.websector.blog.openamf.mapping.vo;
-
-
import java.util.Date;
-
-
public class UserVO
-
{
-
private String userName;
-
private Date registerDate;
-
-
public UserVO() {}
-
-
public String getUserName()
-
{
-
return this.userName;
-
}
-
-
public void setUserName(String value)
-
{
-
this.userName = value;
-
}
-
-
public Date getRegisterDate()
-
{
-
return this.registerDate;
-
}
-
-
public void setRegisterDate(Date value)
-
{
-
this.registerDate = value;
-
}
-
-
}
-
-
-
On the Flash-side you have to point the VO to its server-side “antagonist”
using– that’s important for class mapping on Flash-side –[RemoteClass(alias="de.websector.blog.openamf.mapping.vo.UserVO")]andregisterClassAlias("UserVO", UserVO);to register the VO within Flash. Here an example:UserVO.as ( Download code )
-
package
-
{
-
-
[RemoteClass(alias="de.websector.blog.openamf.mapping.vo.UserVO")]
-
-
import flash.net.registerClassAlias;
-
-
public class UserVO
-
{
-
private var _userName: String;
-
private var _registerDate: Date;
-
-
public function UserVO():void {}
-
-
public function get userName(): String
-
{
-
return this._userName;
-
}
-
-
public function set userName(value: String): void
-
{
-
this._userName = value;
-
}
-
-
public function get registerDate(): Date
-
{
-
return this._registerDate;
-
}
-
-
public function set registerDate(value: Date): void
-
{
-
this._registerDate = value;
-
}
-
-
static public function register():void
-
{
-
registerClassAlias("UserVO", UserVO);
-
}
-
}
-
}
-
-
-
Within the OpenAMF config file named openamf-config.xml you have to configure the behavior of outgoing AMF messages setting the
forceLowerCaseKeys-tag tofalseto avoid converting all keys to lower case.snippet1.xml
-
<amf-serializer>
-
<force-lower-case-keys>false</force-lower-case-keys>
-
</amf-serializer>
-
-
Map within the same openamf-config.xml file all Value Objects located on the server- and Flash-side as follows:
snippet2.xml
-
<custom-class-mapping>
-
<java-class>de.websector.blog.openamf.mapping.vo.UserVO</java-class>
-
<custom-class>UserVO</custom-class>
-
</custom-class-mapping>
-
-
To connect the OpenAMF gateway within Flash you have to create an instance of
flash.net.NetConnection;callingmyNetConnectionInst.connect("http://mysite/openamf/gateway");. To listen to results or faults create aflash.net.Responder;instance and use it for calling server-side services usingmyNetConnectionInst.call("com.yoursite.YourService.serviceMethod", youResponder, parameters);.OpenAMF use the AMF0 protocol so you have to point it using
myNetConnectionInst.objectEncoding = ObjectEncoding.AMF0;.Don’t forget to register the VO with its static
register()method.Here is an example:
OpenAMFMapping.as ( Download code )
-
package {
-
-
import flash.net.Responder;
-
import flash.net.NetConnection;
-
import flash.net.ObjectEncoding;
-
-
import flash.display.Sprite;
-
-
public class OpenAMFMapping extends Sprite {
-
-
private var _nc: NetConnection;
-
-
public function OpenAMFMapping() {
-
-
UserVO.register();
-
-
_nc = new NetConnection();
-
_nc.objectEncoding = ObjectEncoding.AMF0;
-
-
_nc.connect("http://localhost:8080/mappingExample/gateway");
-
-
var responder:Responder = new Responder(onResult, onFault);
-
-
_nc.call("de.websector.blog.openamf.mapping.services.UserServices.getUserByName", responder, "Luke Skywalker");
-
}
-
-
-
private function onFault(result:Object):void {
-
trace("onFault: " + result.toString());
-
}
-
-
private function onResult(result:Object):void {
-
-
trace("onResult: " + result);
-
-
-
try {
-
var user: UserVO = result as UserVO;
-
trace("userName: " + user.userName);
-
trace("userRegisterDate: " + user.registerDate);
-
}
-
catch(error: Error) {
-
trace ("onResult ERROR " + error.message);
-
}
-
}
-
-
}
-
-
}
-
- Tip: For better remoting calls check out Danny Pattersons “AS3 Lightweight Remoting Framework“[UPDATE 10/17/07] or Aaron Smith’s SSR: Super Simple Remoting[/UPDATE] as well.
Screen shot
Mapped results within Flash CS3 debugger calling server-side service getUserByName(String userName):

Download example files
OpenAMFMappingExampleFiles.zip
(Downloads: 839)
Helpful links
- [AS3]
Jorge Solis: Hello World with Openamf
Vishwajit: Sending custom class object thru remoting using OpenAmf - [AS2]
Ginormous Blog: Custom class mapping with openAMF
Darron Schall: Some notes about using OpenAMF - [OS X]
Apple Developer Connection: Java and Tomcat on Mac OS X, Part I


September 24th, 2007 at 5:35 am
Clear DataBuilder plugin does automatic configuration of OpenAM/LCDS/FDS and generates both client and server side code for communication. We also added hot deployment and configuration and generalized RemoteObject that can be used for Adobe RPC or openAmf. It is also very simple to add additional protocols. In addition, recognised standard ones like WebServices are automatically proxied over RPC as long as destination has wsdl suffix and proxy is configured.
Regards,
Anatole
September 24th, 2007 at 7:00 am
Anatole,
thanks for your hint. One question: Does Clear DataBuilder work with “pure” Flash apps (without the Flex framework)?
-sectore
October 16th, 2007 at 6:14 pm
There is a really nice remoting package called SSR (ssr.riaforge.org) It’s much simpler than as3lrf.
October 16th, 2007 at 6:33 pm
actually the ssr project moved to osflash.org/projects/ssr
October 17th, 2007 at 7:57 am
Aaron,
thanks for your hint! I’ve added a link to yours SSR: Super Simple Remoting, too
-sectore
October 23rd, 2007 at 10:28 pm
I am new to AS3. I have one question about the statement in this article which says “On the Flash-side you have to point the VO to its server-side “antagonist†using [RemoteClass(alias="de.websector.blog.openamf.mapping.vo.UserVO")]”
I was playing around with this example and I removed this line from my code and still everything works fine for me. I can send and receive object from server even without this line.
Can anyone explain me why do we need to put this line in our code and what exactly it does ?
Any help is appreciated.
Thanks,
Manan
October 24th, 2007 at 9:23 am
Manam,
you’re right – it seems that the meta tag named RemoteClass is needed for Flex only instead using
registerClassAlias(“com.mydomain.vo.MyServerSideVO”, MyFlashVO);
Using Flash you have to use the registerClassAlias method mentioned above without the metag [RemoteClass(alias="com.mydomain.vo.MyServerSideVO")]. The Flash IDE compiler ignores this metatag.
Thanks for your hint!
-sectore
November 29th, 2007 at 8:09 pm
[...] The following test example based on my Flash Remoting example using OpenAMF described at the previous article called “Best practices: Custom class mapping using OpenAMF and AS3 (Flash CS3 – not Flex)” [...]
March 14th, 2008 at 7:34 pm
I create the following function
———————————
package com.cerebrum.utils {
import mx.rpc.events.ResultEvent
import mx.controls.Alert;
public class Result {
[Embed("/assets/images/dialog-information.png")]
public var IconDialogInformation:Class;
public function Result(event:ResultEvent,MyObject:Object):void {
Alert.show(“teste”, “teste”, 4, null, null, IconDialogInformation);
}
}
}
———————————
I as follows
import Result;
Result(event, meuobjeto);
———————————
Is presented in the following error flex builder
1137: Incorrect number of arguments. Expected no more than 1.
———————————
How should I inform the function that actually has 2 parameters?
October 15th, 2008 at 10:24 pm
It’s not working for me! Can anyone send me a zipped file with the files that need to be in the Tomkat webapps folder?
For some reason the flash side is not reaching the java class, the UserVO object is never filled…
Please help!!!
October 16th, 2008 at 8:22 am
Rogelio (or Adrain
),
I’ve already sended all files to you using your email adress hosted at advanceme.com
Anyway, you can ping me an email again.
-Jens
February 14th, 2009 at 10:57 pm
I have some AS3 problem at CS4 (1137: Incorrect number of arguments. Expected no more than 0.)
var currentColumn:int = i -(currentRow * numColumns);
“”aSpinners[i]= new Spinner(i);”"
gameScreen.addChild(aSpinners[i]);
aSpinners[i].x=currentColumn*hSpace;
aSpinners[i].y=currentRow*vSpace;
October 21st, 2009 at 1:20 pm
OpenAMF is a good library for AMF communications, I write a simple tutorial to get in communication servlet with as3 client side.
http://programmaremobile.blogspot.com/2009/10/java-and-amf-by-as3-in-flash.html
The problem is that OpenAMF supports only the AMF0 format, that is old. I don’t like to use older version of a tecnologies
June 15th, 2010 at 9:26 am
Hi Sector,
According to Manan question.. why then do most ppl insert the
[RemoteClass()] in their code if it is not really necessary? or when is it necessary and when is it not needed?