转自http://gregoire.org/2008/09/12/using-custom-objects-in-amf3-with-red5/
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...