AS3无法从Java发送的套接字解码AMF3对象
作者:互联网
我有一个java套接字服务器,它在连接时将Animal对象发送到Flash客户端.
对象发送方式如下:
Amf3Output amf3Output = new Amf3Output(SerializationContext.getSerializationContext());
amf3Output.setOutputStream(userSocket.getOutputStream());
amf3Output.writeObject(animal);
闪存方面的代码是:
var object:Object = socket.readObject();
trace(object);
trace(object as Animal);
但是,当第二个跟踪给我一个null
我检查过java发出31个字节,Flash接收31个字节.
我认为可能是我的Java和AS3类与某些AMF要求不匹配.
Java类:
package main;
public class Animal {
public String name;
public int age;
}
AS3课程:
package
{
[Bindable]
[RemoteClass(alias="main.Animal")]
public class Animal
{
public var name:String;
public var age:int;
}
}
解决方法:
我不熟悉Java和可用的AMF序列化器/反序列化器,但是在闪存中确实支持通过套接字发送类型化对象,并且如果发送正确的数据则可以正常工作.下面是ruby与Flash应用程序通信的套接字服务器的示例.我正在使用RocketAMF在连接后通过套接字将AMF3对象发送到客户端.
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
}
标签:java,flex,serialization,actionscript-3,amf 来源: https://codeday.me/bug/20190704/1380400.html