[转] Using custom objects in AMF3 with Red5

转自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:

AS3
 
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:

Java
 
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.

AS3
 
    // 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.

Java
 
    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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值