AS3 无法从 Java 发送的套接字解​​码 AMF3 对象

发布于 2024-11-18 12:23:29 字数 860 浏览 3 评论 0原文

我有一个 java 套接字服务器,它在连接时将 Animal 对象发送到 Flash 客户端。 对象是这样发送的:

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

Flash 端的代码是:

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;

    }

}

I have a java socket server that sends an Animal object to the Flash client when it connects.
The object is sent like this:

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

And the code on flash side is:

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

However when the second trace gives me a null

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

I think it might be that my Java and AS3 classes don't match some AMF requirement.

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;

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梓梦 2024-11-25 12:23:29

我不熟悉 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
}

I'm not familiar with Java and the AMF serializers/deserializers available for it, but sending typed objects over sockets is indeed supported in flash, and works properly if you send the right data. Below is an example of a socket server in ruby communicating with a Flash application. I'm using RocketAMF to send an AMF3 object over the socket to a client after it connects.

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
}
小忆控 2024-11-25 12:23:29

您是否检查过 ActionScript 端的 objectEncoding 设置为 3?如果您发送 AMF3 数据并且它尝试读取 AMF0 数据,则它将无法工作。

Have you checked that the objectEncoding is set to 3 on the ActionScript side? If you're sending AMF3 data and it's trying to read AMF0 data, it won't work.

盛夏已如深秋| 2024-11-25 12:23:29

由于您已经使用了 BlazeDS,因此很难确定 AMF3 要求与 BlazeDS 要求是什么。我要说的是,BlazeDS 需要遵循 Java Beans 规范的 Java 类,这意味着匹配 getter 和 setter。

我还假设 Flex 端需要更多的东西来反序列化 AMF3 有效负载:在线格式不是 Actionscript 3.0 对象。我已经完成了一些 BlazeDS 自定义序列化,这就是为什么我认为您在 Flex 方面缺少一些东西。

您不使用 BlazeDS(或 GraniteDS)进行通信有什么原因吗?

Since you've taken parts of BlazeDS, it's hard to determine what are AMF3 requirements versus BlazeDS requirements. What I will say is that BlazeDS needs Java classes that follow the Java Beans spec and that means matching getters and setters.

I'm also assuming that more is needed on the Flex side to de-serialize the AMF3 payload: the on-the-wire format is not an Actionscript 3.0 object. I've done some BlazeDS custom serialization which is why I think you're missing something on the Flex side.

Is there a reason you're not using BlazeDS (or GraniteDS) for the communication?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文