If you stuck with JSON as3corelib would help you on the client side like
yourObject={now:new Date()}
room.sendModuleMessage("TESTJSON",{data:JSON.encode(yourObject));
but not on the server side.
String input=evt.getMessage().getArg("data");
JSONObject data = new JSONObject(input);
data.getString("now")
In attachement you will find json.jar library. Put it in "lib" of your union instalation and add it to the startserver.bat like lib\json.jar;
I'm bit tightent with scarce JAVE Message interface having 3 not so useful methods(designed only for dynamic Objects) in discussed cases. I hope in future Message would be more customizable in the future so we could send custom structure of the message content and we could write more optimize code instead writing hacks :)).
This made me problems when I tried
room.sendModuleMessage("TESTAMF",{data:AMF.serializring(classObject)); and to use byteArray.compress(), cos
Message content has much more chars above the 64 ASCII array or chars, so it makes big problems in getArg/s parsers of Message obejct to produce something.
The solution was
CurrentDayVO classObject=new CurrentDayVO();
classObject.now = new Date();
room.sendModuleMessage("TESTAMF",{data:Base64.encode(AMFSerializer.serializeToString(classObject)));
In attachement you can find AMFSerializer.as and Base64.as for client side and for server side
you need to include into lib folder of your union instalation BlazeDS flex-messaging-core.jar;flex-messaging-common.jar;(also attached )
and in startserver.bat lib\flex-messaging-core.jar;lib\json.jar;lib\flex-messaging-common.jar; so you can use
AMFSerialize.java like this:
String input=evt.getMessage().getArg("data");
input=Base64decoder.decode(input);
CurrentDayVO output = AMFSerializer.fromAmf(input);
For sending complex class object you need to make 2 classes one in AS3(CurrentDayVO.as) and one in Java(CurrentDayVO.java).
AS3:
package com.jdftm.vo{
[Bindable]
//[RemoteClass(alias="com.jdftm.vo.CurrentDayVO")]
public class CurrentDayVO{
private var _now:Date;
public function get now():Date{
return _now;
}
public function set now(value:Date):void{
_now=value;
}
}
}
JAVA:
package com.jdftm.vo;
import java.util.Date;
public class CurrentDayVO
{
private Date now;
public CurrentDayVO() {
}
public void setNow(Date now) {
this.now = now;
}
public Date getNow() {
return now;
}
}
They need to be in same packages. Dont forget to
registerClassAlias("com.jdftm.vo.CurrentDayVO", CurrentDayVO);
in you client code so compiler know to preserve Class object strucuture during serialization.You need to put CurrentDayVO.class (compiled java) into your "lib" folder of union instalation.
TestUnion.as is the basic client application that sends JSON and AMF packages.
TestRoomModule.java is custom room module handling unpack and use of JSON or AMF messages sent by the client and send back.
public void TestJson(RoomEvent evt)
{
String input=evt.getMessage().getArg("data");
JSONObject data = null;
//Map msgMap=evt.getMessage().getArgs();
System.out.println("---------------------------------------");
System.out.println("Received JSON Test Package:"+input);
try {
data = new JSONObject(input);
} catch (JSONException e1) {
// TODO Auto-generated catch block
System.out.println(e1.getMessage());
}
try {
System.out.println("Data from JSON Obeject "+data.getString("now"));
} catch (JSONException e1) {
// TODO Auto-generated catch block
System.out.println(e1.getMessage());
}
try {
//{echo:ofTheMessage,key:"We User1 know what JSON is!"}
data.put("echo",input);
data.put("key", "We User1 know what JSON is!");
m_ctx.getRoom().sendMessage("MODULE_RESPONSE", data.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void TestAmf(RoomEvent evt)
{
String input=evt.getMessage().getArg("data");
//Map msgMap=evt.getMessage().getArgs();
byte[] iBytes=null;
CurrentDayVO output=null;
System.out.println("---------------------------------------");
System.out.println("Received AMF Test Package:"+input);
/* try {
iBytes = ZIPCompressor.uncompress(input.getBytes());
} catch (DataFormatException e1) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
try {
input=new String(iBytes, 0, iBytes.length, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
//evt.getMessage().
System.out.println("uncompressed:"+input);
*/
decoder.reset();
decoder.decode(input);
iBytes=decoder.flush();
try {
input=new String(iBytes, 0, iBytes.length, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
System.out.println("AMF:"+input);
try {
output = AMFSerializer.fromAmf(input);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
System.out.println("Data from CurrentDayVO getNow()"+output.getNow().toString());
/*System.out.println(input);
System.out.println(evt.toString());*/
}
Source download
No comments:
Post a Comment