AS3 cannot decode AMF3 object from socket sent by Java

I have a Java socket server that sends an Animal object to a Flash client when it connects. The object is sent as follows:

Amf3Output amf3Output = new Amf3Output(SerializationContext.getSerializationContext()); amf3Output.setOutputStream(userSocket.getOutputStream()); amf3Output.writeObject(animal); 

And the code from the flash side:

 var object:Object = socket.readObject(); trace(object); trace(object as Animal); 

However, when the second trace gives me null

I checked that java sends 31 bytes and Flash receives 31 bytes.

I think it is possible that my Java and AS3 classes are not AMF compliant.

Java class:

 package main; public class Animal { public String name; public int age; } 

AS3 Class:

 package { [Bindable] [RemoteClass(alias="main.Animal")] public class Animal { public var name:String; public var age:int; } } 
+4
source share
3 answers

I am not familiar with Java and the AMF serializers / deserializers available to it, but sending typed objects over sockets is really supported in flash and works correctly if you send the correct data. The following is an example of a ruby ​​socket server that interacts with a Flash application. I use RocketAMF to send an AMF3 object through a socket to the client after it is connected.

SocketTest.as:

 package { import flash.display.Sprite; import flash.net.registerClassAlias; import org.rackAMF.*; import flash.net.Socket; import flash.events.*; public class SocketTest extends Sprite { private var socket:Socket; public function SocketTest() { registerClassAlias('Animal', Animal); socket = new Socket(); socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse); socket.connect("localhost", 8081); } private function onResponse(e:ProgressEvent):void { var animal:Animal = socket.readObject() as Animal; trace(Object(animal).constructor); // [trace] [class Animal] trace(animal.name); // [trace] Zebra trace(animal.age); // [trace] 5 } } } class Animal { public var name:String; public var age:int; } 

socket_server.rb:

 require 'rubygems' require 'socket' require 'rocketamf' class Animal attr_accessor :name, :age end # Map "Animal" in ruby to "Animal" in flash RocketAMF::ClassMapper.define do |m| m.map :as => 'Animal', :ruby => 'Animal' end server = TCPServer.open(8081) loop { client = server.accept animal = Animal.new animal.name = "Zebra" animal.age = 5 client.write RocketAMF.serialize(animal, 3) client.close } 
+2
source

Have you noticed that objectEncoding set to 3 on the ActionScript side? If you send AMF3 data and try to read AMF0 data, this will not work.

0
source

Since you took parts of BlazeDS, it is difficult to determine which AMF3 requirements and BlazeDS requirements. I would say that BlazeDS needs Java classes that follow the Java Beans specification, which means matching receivers and setters.

I also assume that the Flex side needs more to de-serialize the AMF3 payload: the inline format is not an ActionScript 3.0 object. I did some serialization of BlazeDS, so I think you have something missing on the Flex side.

Is there a reason you are not using BlazeDS (or GraniteDS) for communication?

0
source

All Articles