I created a post about this subject almost a year ago, but there were a couple minor issues with the examples. Here I will show what eight additional months of experience can provide. The example provided here uses a custom object in Flex to pass information to and from the server, which in this case will be Red5. If one of you has an example which uses FMS on the server-side, I would be glad to include it here.
First create an object to hold your data:
package mypackage { import flash.utils.IDataInput; import flash.utils.IDataOutput; import flash.utils.IExternalizable; import mx.utils.*; [Bindable] [RemoteClass(alias="mypackage.MyObject")] public class MyObject implements IExternalizable { private var l:Number; private var x:int; private var b:int; private var bool:Boolean; private var str:String; public function getL():Number { return l; } public function setL(l:Number):void { this.l = l; } public function getX():int { return x; } public function setX(x:int):void { this.x = x; } public function getB():int { return b; } public function setB(b:int):void { this.b = b; } public function getBool():Boolean { return bool; } public function isBool():Boolean { return bool; } public function setBool(bool:Boolean):void { this.bool = bool; } public function getStr():String { return this.str; } public function setStr(str:String):void { this.str = str; } public function readExternal(input:IDataInput):void { l = input.readUnsignedInt(); x = input.readInt(); b = input.readByte(); bool = input.readBoolean(); str = input.readUTF(); } public function writeExternal(out:IDataOutput):void { out.writeUnsignedInt(l); out.writeInt(x); out.writeByte(b); out.writeBoolean(bool); out.writeUTF(str); } }
Now create the server-side version of the object:
package mypackage; import org.red5.io.amf3.IDataInput; import org.red5.io.amf3.IDataOutput; import org.red5.io.amf3.IExternalizable; /** * Sample class */ public class MyObject implements IExternalizable { private static final long serialVersionUID = 11520080920; private Long l; private Integer x; private Byte b; private Boolean bool; private String str; public Long getL() { return l; } public void setL(Long l) { this.l = l; } public Integer getX() { return x; } public void setX(Integer x) { this.x = x; } public Byte getB() { return b; } public void setB(Byte b) { this.b = b; } public Boolean getBool() { return bool; } public Boolean isBool() { return bool; } public void setBool(Boolean bool) { this.bool = bool; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } /** * Deserializes the client state of an instance. */ @Override public void readExternal(IDataInput in) { l = (Long) in.readObject(); x = in.readInt(); b = in.readByte(); bool = in.readBoolean(); str = in.readUTF(); } /** * Serializes the server state of an instance. */ @Override public void writeExternal(IDataOutput out) { out.writeObject(l); out.writeInt(x); out.writeByte(b); out.writeBoolean(bool); out.writeUTF(str); } }
Now that we have the client-side and server-side objects, lets request one from the server. The example assumes that you have a NetConnection present.
// call server-side method
public function getMyObject():void {
// create a responder set to callback
var resp:Responder = new Responder(handleResp, null);
// call the server side method
nc.call("getMyObject", resp, null);
}
//callback handler
public function handleResp(o:Object):void {
myObj = o as MyObject;
}
Create a request handler on the server-side. This assumes that you have Red5 set up and have created an Application.
public MyObject getMyObject(String param) {
MyObject myObj = new MyObject();
myObj.setL(1L);
myObj.setX(42);
myObj.setB((byte) 1);
myObj.setBool(true);
myObj.setStr("The quick brown fox");
return myObj;
}
Thats all folks, seems simple now that I've done it a bazillion times...
Tags: amf, as3, externalizable, Java, Red5, remoteclass
Paul,
A problem I ran into a while back is serializing arrays in a custom object and sending that custom object to and from Red5. I could write the custom object on the Red5 side and send it to Flash, but I couldn’t send the custom object to Red5 from Flash. I tried casting the object returned by readObject() as multiple things, first a strict Object, next an Object[], next a Collection, then a List, then a Set… and none of it worked.
What I had to do is log out the value of the object’s getClass function. Turns out that Red5 serialized arrays serialized in custom objects as ArrayLists. Cast your readObject() result as an ArrayList and you should be fine, ie:
myList = (ArrayList) input.readObject();
- TK
View this Comment in:

very good sample
tnx
View this Comment in:

i tried the example with flash cs3, because i could not use mx.utils.RemoteClass i tried my luck with “registerClassAlias” instead but receiving an Error #2030, reached end of file
at flash.utils::ObjectInput/readUTF()
at myPackage::myObject/readExternal()
will keep on trying, there are less examples for flash cs3.
one question, needed to add a “}” rightbrace at the end of “public class MyObject implements IExternalizable”, may it be that its missing there?
Greetz
Sascha
View this Comment in:
