如何通过 NetConnection 将复杂类型中继到 FMS?

发布于 2024-10-18 20:21:40 字数 1306 浏览 0 评论 0原文

我需要通过 NetConnection 将复杂类型对象(在 Flex 中标记为 RemoteClass)发送到其他客户端。

[RemoteClass]
public class ComplexType
{
    public var _someString:String;
    public var _someInt:int;
}

...并使用...

_nc = new NetConnection();
_nc.connect("rtmp://localhost/echo/");
_nc.addEventListener(NetStatusEvent.NET_STATUS, _onNetStatus);              
_nc.client = {};
_nc.client.echoCallback = _echoCallback;

var dto:ComplexType = new ComplexType();
dto._someInt = 4;
dto._someString = "abrakadabra";                    
_nc.call("echo", null, dto);

但是,服务器端的回调函数似乎不理解强类型对象并发送回以下内容:

private function _echoCallback(...args):void
{
    trace(ObjectUtil.toString(args));
    /*

    (Array)#0
      [0] (Object)#1
         _someInt = 4
         _someString = "abrakadabra"

    */
}

服务器端看起来像这样:

application.onAppStart = function () {
    trace("Application.onAppStart > application started");

    Client.prototype.echo = function (complexType /*ComplexType*/) {
        trace("Client.echo > calling echo");        
        application.broadcastMsg("echoCallback", complexType);
    }
}

有没有办法通过 NetConnection 中继强类型对象?

EDIT1:添加带有 ObjectUtil.toString() 输出的回调函数源代码

I need to send complex type object (marked RemoteClass in Flex) via NetConnection to other clients.

[RemoteClass]
public class ComplexType
{
    public var _someString:String;
    public var _someInt:int;
}

... and using ...

_nc = new NetConnection();
_nc.connect("rtmp://localhost/echo/");
_nc.addEventListener(NetStatusEvent.NET_STATUS, _onNetStatus);              
_nc.client = {};
_nc.client.echoCallback = _echoCallback;

var dto:ComplexType = new ComplexType();
dto._someInt = 4;
dto._someString = "abrakadabra";                    
_nc.call("echo", null, dto);

However it seems, that callback function on server side don't understand strongly typed objects and sends back this:

private function _echoCallback(...args):void
{
    trace(ObjectUtil.toString(args));
    /*

    (Array)#0
      [0] (Object)#1
         _someInt = 4
         _someString = "abrakadabra"

    */
}

Server side looks like this:

application.onAppStart = function () {
    trace("Application.onAppStart > application started");

    Client.prototype.echo = function (complexType /*ComplexType*/) {
        trace("Client.echo > calling echo");        
        application.broadcastMsg("echoCallback", complexType);
    }
}

Is there a way to relay strongly typed object via NetConnection?

EDIT1: added callback function source code with ObjectUtil.toString() output

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

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

发布评论

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

评论(2

漆黑的白昼 2024-10-25 20:21:41

您需要将 alias 属性添加到 [RemoteClass] 注释中:

[RemoteClass(alias="my.unique.Class")]

这应该将匿名对象更改为 AMF 中的类型化对象。

You need to add an alias property to your [RemoteClass] annotation:

[RemoteClass(alias="my.unique.Class")]

This should change the anonymous object to a typed object in AMF.

吾性傲以野 2024-10-25 20:21:41

用于发送用途:

var ba:ByteArray = new ByteArray();
ba.writeObject(dto);

_nc.call("echo", null, ba);

用于接收:

private function _echoCallback(ba:ByteArray):void
{
    var dto:ComplexType = ba.readObject() as ComplexType;

    trace(ObjectUtil.toString(dto));

    /*
    (ComplexType)#0
      _someInt = 4
      _someString = "abrakadabra"
    */
}

太棒了!!!!

For sending use:

var ba:ByteArray = new ByteArray();
ba.writeObject(dto);

_nc.call("echo", null, ba);

And for receiving:

private function _echoCallback(ba:ByteArray):void
{
    var dto:ComplexType = ba.readObject() as ComplexType;

    trace(ObjectUtil.toString(dto));

    /*
    (ComplexType)#0
      _someInt = 4
      _someString = "abrakadabra"
    */
}

It woooorks!!!!

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