msgbartop
Various ramblings-on, mostly about Red5
msgbarbottom

12 Sep 08 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…

Tags: , , , , ,



Reader's Comments

  1. |

    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

  2. |

    very good sample
    tnx

  3. |

    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

  4. |

    hello,

    I have used you example as template for implementing my Own Chat functionality but i am getting exception wheile getting data from server.

    ArgumentError: Error #2173: Unable to read object in stream. The class ServerObjects.chatObject does not implement flash.utils.IExternalizable but is aliased to an externalizable class.

    can you please help me out here.

  5. |

    Thank you so much, it is working well! May I ask how one would send such an object TO the server? I want the server to place my custom defined objects into an array of objects. Got any ideas?

  6. |

    Why it doesn’t work when there is CUSTOM object like Client instead of native type like boolean, string, number ? I get an error from Flash The index is out of bound!!!!

    In my class i have other classes

    Like this:

    [Bindable]
    public class CustomVariableBase implements IExternalizable {

    private var _customVariable:CustomVariable;
    private var _id:Number;
    private var _name:String;
    private var _parentId:Number;

    ……..

    @Override
    public void readExternal(IDataInput input) {
    // TODO Auto-generated method stub
    customVariable = (CustomVariable) input.readObject();
    id = (Long) input.readObject();
    name = input.readUTF();
    parentId = (Long) input.readObject();
    //variablesValues = (List) input.readObject();
    }

    @Override
    public void writeExternal(IDataOutput output) {
    // TODO Auto-generated method stub
    output.writeObject(customVariable);
    output.writeObject(id);
    output.writeObject(name);
    output.writeObject(parentId);
    //output.writeObject((Object)variablesValues.toArray());
    }

  7. |

    if get this specific error :

    Main Thread (Suspended: RangeError: Error #2006: The supplied index is out of bounds.)

    when i pass the object via an objectmap like this : map.put(“custom_variables”, my_manager.getCustomVariablesAsObjects());

  8. |

    I get following error:

    ArgumentError: Error #2173: Unable to read object in stream. The class ServerObjects.chatObject does not implement flash.utils.IExternalizable but is aliased to an externalizable class.

    Why is it not working for me? Dominick’s solution doesn`t work either:
    http://dominick.infrared5.com/?p=118#more-118

  9. |

    Hello Paul, I have one problem in a similar case like this.
    I am working with Print2Flash API, RED5 and AS3. I have and aplication where user A and user B are sharing one document loaded with Print2Flash API (http://print2flash.com/help/DocumentAPIFunctions.php) and I want to pass objects of this API between users A and B.
    The problem is that this Print2Flash objects can not be parsed. How can I pass this objects?
    My code is this in my client-side class:
    private function onVisibleAreaChanged(e:Object):void
    {
    ro.sendAreaVisible(P2FDoc.getVisibleArea());
    }
    private function visibleArea(e:Event):void
    {
    P2FDoc.setVisibleArea(Object(e.target.docObj))
    }
    My code in server-side class is:
    public function sendAreaVisible(obj:Object):void
    {
    so.send(“areaVisible”, obj);
    }
    public function areaVisible(obj:Object):void
    {
    docObj = obj;
    dispatchEvent(new Event(“onVisibleArea”, docObj));
    }

    Could you help me?
    Thank you very much

  10. |

    Didn’t know the forum rules allewod such brilliant posts.



Leave a Comment


Fatal error: Call to undefined function akismet_counter() in C:\xampp\htdocs\paulgregoireblog\wp-content\themes\googlechrome\footer.php on line 9