header
ask question
Click here to ask Question Now Its free No registration required. Flash, Flex, Flash Media Server, ActionScript,Adobe Air. Most questions receive a response in an hour.
John Russell
Points:0
Posts:0
Answered

5/6/2010 12:31:24 PM

Title: Sending custom defined class from AS3 to Red5


Hello!
I am currently trying to pass a custom defined class from AS3 to Red5. It should be noted that I have already successfully been able to to receive a custom defined class from Red5 to AS3 using this guys guide: http://gregoire.org/2008/09/12/using-custom-objects-in-amf3-with-red5/. The problem lies in the fact that when the server sends me an object, flash appears to properly serialize it to the cass, but all the values within it are NULL.

//My client code passing the object is:

nc.call("updateObjectData",null, myObject);

//My server code on Red5 is:
public void updateObjectData(Object input)
    {       
        ObjectType temp = new ObjectType ();
        temp = (ObjectType) input;     // temp returns of type ObjectType but all values inside are NULL    
    }
Any thoughts are much appreciated. I have scoured the internet for days looks for an answer to no avail. Most guides show how to go from server to client, but not the other way around.



1
Inder
Points: 2880
Posts:0
5/6/2010 10:31:41 PM



you should be able to receive your object parameters on Server side of RED5 like this


public void updateObjectData(Object[] params)
{
boolean firstVaraible = (Boolean)params[0];
String secondVariable =  (String)params[1];
} 

2
John Russell
Points: 0
Posts:0
5/9/2010 3:33:51 PM



Hello! Thanks for the response. However, I am not actually receiving those as parameters. Instead, the only thing I get on the java server is:

params[0] = org.red5.core.ObjectTypee@1a76be4

When I try casting any of the parameters to strings or boolean or ints, they throw errors obviously. Below is my class in java, and I will then show you my class in AS3. They are of type LobbyType. Thank you so much for you help on this thus far, I have been stuck on this for so long!

// Java
package org.red5.core;

import org.red5.io.amf3.IExternalizable;
import org.red5.io.amf3.IDataInput;
import org.red5.io.amf3.IDataOutput;

public class LobbyType implements IExternalizable
{
	
	public String redUser;			// red5 username
	public int uid;					// fb uid
	public String fname;			// fb name
	public String pic;				// fb pic
	public Boolean intbl;			// is in the table?
	public int t;					// table number
	public int s;					// seat number
	
	
	public LobbyType()
	{	
	}
		
	public void setRedUser(String redUser)
	{
		this.redUser = redUser;
	}
	
	public String getRedUser()
	{
		return redUser;
	}
	
	public void setUid(int uid)
	{
		this.uid = uid;
	}

	public int getUid()
	{
		return uid;
	}

	public void setFname(String fname)
	{
		this.fname = fname;
	}
	
	public String getFname()
	{
		return fname;
	}
	
	public void setPic(String pic)
	{
		this.pic = pic;
	}
	
	public String getPic()
	{
		return pic;
	}
	
	public void setIntbl(Boolean intbl)
	{
		this.intbl = intbl;
	}	
	
	public Boolean getIntbl()
	{
		return intbl;
	}	
	
	public void setTable(int t)
	{
		this.t = t;
	}
	
	public int getTable()
	{
		return t;
	}
	
	public void setSeat(int s)
	{
		this.s = s;
	}

	public int getSeat()
	{
		return s;
	}
	
	public void readExternal(IDataInput input)
	{
		redUser = input.readUTF();
		uid = input.readInt();
		fname = input.readUTF();
		pic = input.readUTF();
		intbl = input.readBoolean();
		t = input.readInt();
		s = input.readInt();
	}
	
	public void writeExternal(IDataOutput output)
	{
		output.writeUTF(redUser);
		output.writeInt(uid);
		output.writeUTF(fname);
		output.writeUTF(pic);
		output.writeBoolean(intbl);
		output.writeInt(t);
		output.writeInt(s);
	}
}

// AS 3 

package 
{
	import flash.utils.IDataInput;
	import flash.utils.IDataOutput;
	import flash.utils.IExternalizable;
	import flash.net.registerClassAlias;

	public class LobbyType implements IExternalizable
	{
		public var redUser:String;// red5 username
		public var uid:int;// fb uid
		public var fname:String;// fb name
		public var pic:String;// fb pic
		public var intbl:Boolean;// is in the table?
		public var t:int;// table number
		public var s:int;// seat number


		public function LobbyType():void
		{
		}

		public function registerObject():void
		{
			registerClassAlias("org.red5.core.LobbyType", LobbyType);
		}


		public function setRedUser(redUser:String):void
		{
			this.redUser = redUser;
		}

		public function getRedUser():String
		{
			return redUser;
		}

		public function setUid(uid:int):void
		{
			this.uid = uid;
		}

		public function getUid():int
		{
			return uid;
		}

		public function setFname(fname:String):void
		{
			this.fname = fname;
		}

		public function getFname():String
		{
			return fname;
		}

		public function setPic(pic:String):void
		{
			this.pic = pic;
		}

		public function getPic():String
		{
			return pic;
		}

		public function setIntbl(intbl:Boolean):void
		{
			this.intbl = intbl;
		}

		public function getIntbl():Boolean
		{
			return intbl;
		}

		public function setTable(t:int):void
		{
			this.t = t;
		}

		public function getTable():int
		{
			return t;
		}

		public function setSeat(s:int):void
		{
			this.s = s;
		}

		public function getSeat():int
		{
			return s;
		}


		public function readExternal(input:IDataInput):void
		{

			redUser = input.readUTF();
			uid = input.readInt();
			fname = input.readUTF();
			pic = input.readUTF();
			intbl = input.readBoolean();
			t = input.readInt();
			s = input.readInt();
		}


		public function writeExternal(output:IDataOutput):void
		{

			output.writeUTF(redUser);
			output.writeInt(uid);
			output.writeUTF(fname);
			output.writeUTF(pic);
			output.writeBoolean(intbl);
			output.writeInt(t);
			output.writeInt(s);

		}

	}

}

3
John Russell
Points: 0
Posts:0
5/9/2010 3:41:15 PM



This is the error I get from the server:


[ERROR] [NioProcessor-1] org.red5.server.service.ServiceInvoker - Error executing call: Service: null Method: updateObjectData Num Params: 1 0: org.red5.core.LobbyType@18fad4b
[ERROR] [NioProcessor-1] org.red5.server.service.ServiceInvoker - Service invocation error
java.lang.reflect.InvocationTargetException: null


4
Rayan
Points: 690
Posts:0
5/9/2010 9:06:22 PM



it seems you are not parsing the object the right way , if you want to get parameters out of your object you need to use
param0= org.red5.core.ObjectTypee@1a76be4[0]
param1 = org.red5.core.ObjectTypee@1a76be4[1]

it might also be possible that ObjectTypee@1a76be4[0] is also object further so casting wont work. try ObjectTypee@1a76be4[0][0] also.

5
Inder
Points: 2880
Posts:0
5/9/2010 9:34:34 PM



org.red5.core.LobbyType@18fad4b is a class org.red5.core.LobbyType, If I am right ? I don't think you can send a class to server side and then extract its properties. Only objects are supported. You can cast the Object org.red5.core.LobbyType@18fad4b to class LobbyType and then you might be able to access the properties.

you mentioned that you are making call (nc.call("updateObjectData",null, myObject);) from actionscript but I cant see any call used in the code you posted.

6
John Russell
Points: 0
Posts:0
5/9/2010 9:55:53 PM



I am so sorry for the confusion. I was using 'ObjectType' as an example, but my actual object is 'LobbyType'.

@Indier - You are correct. I am currently trying to cast the object to type LobbyType, but it is not working. Below I will demonstrate exactly what is happening:

1) The client code on AS3 has created an object called testObject and is of type LobbyType. It makes a call through the net connection using the following:



public function sendSomething():void
{
testObject = o as LobbyType;
// setters omitted to save space, I just filled the object with random data
testObject.registerObject();
ncLobby.call("updateLobbyType",null, testObject);
}


2) The red5 server successfully receives a net connection call to the following function:

ps: I used the getClass() function to show that it does in fact belong to type LobbyType


public void updateLobbyType(Object params)
{
LobbyType temp = new LobbyType();
System.out.println("It is: " + params.getClass().toString());

// It is: org.red5.core.LobbyType
}


It should be noted that I have tried implementing Rayan's idea by casting the object but to no avail...


public void updateLobbyType(Object params)
{
LobbyType temp = new LobbyType();
params.getClass().cast(temp);
System.out.println("It is: " + temp.getUid()); // getUid() is just a getter in the class

// It is: org.red5.core.LobbyType@11da1d1
}


But it doesn't seem to allow me to access any of the member values. I have also tried what you mentioned by doing:


public void updateLobbyType(Object[] params)
{

LobbyType temp = new LobbyType();    	
 	String red5User = (String) params[0];
    	int uid=Integer.parseInt(params[1].toString());
    	String fname = (String) params[2];
    	String pic = (String) params[3];
    	Boolean inttbl = (Boolean) params[4];
    	int t=Integer.parseInt(params[5].toString());
    	int s=Integer.parseInt(params[6].toString());
}

but only params[0] contains data which is just 'org.red5.core.LobbyType@11da1d1', the other objects in the array do not exist. I guess my question is how do I send an object from AS3 to the Java server and access the members of that object class??

7
John Russell
Points: 0
Posts:0
Accepted Answer
5/9/2010 10:26:45 PM



I got it! There was two things that I did.

1) I removed the IExternalizable function because I am only sending simple data types, I figured I would remove that..

2) After doing that, I went back to how I originally wrote the function which was like this and it worked:


public void updateLobbyChat(LobbyType params)
{
someStringType = params.getFname();
}
Thanks again for all your help. It is so awesome to think complete strangers are willing to help others. :)

8
Inder
Points: 2880
Posts:0
5/9/2010 10:38:56 PM



Hi john, As far as I know sending anything other than simple data types is not supported by AMF (ActionScript Media Format). I have not tried it with RED5 but I have tried something similar to this in one of my FMS Application. I tried to send bitmap object (byte Stream Object to server) through server call by passing as parameter. The server will receive the Object but the actual type is lost. So finally after lot of study I found that Flash when communicating with server side cannot send or receive complex data types, like class , Bitmap data, Array collection etc.

When you pass a class from client its very unlikely that its members can be accessed from server and that too in java because its as3 class. How ever I will try to find if anything similar could be found and will post you back. Please do share if you found any solution to you problem.

9
Inder
Points: 2880
Posts:0
5/9/2010 10:44:14 PM



cool this is great. you are welcome, Knowledge is for sharing and we gain always by sharing it. Like I just Gained :)

10
paul
Points: 0
Posts:0
12/27/2010 8:18:29 AM



also see this link
http://gregoire.org/2008/09/12/using-custom-objects-in-amf3-with-red5/


Post your Reply
Name  

Email

Type your Reply or Answer

Are you human? What is 0+3 



Members Login

Email  
Password
Forgot Password





This website focus on: Flash | Flex | FMS | RED5 | WOWZA | Flash Media Server | Adobe AIR | ActionScript,Flash Solutions | Flash Question | Flash Answers | Flash Developers | Flash Problem, Flash Help, Flash bugs, Flash workaround | Flash Blog | Flex Question Answers | Flash Forum | Flex Development | Actionscript development | Flash development | Adobe AIR development
Copyright © 2008 AskMeFlash.com. All rights reserved. Privacy Policy | Terms & Conditions