共享对象发送方法()

时间:2013-05-09 21:51:36

标签: actionscript-3 shared-objects

在使用共享object.send()方面,Adobe文档中似乎存在错误。我正在尝试对所有客户端执行send方法。

我从Adobe复制了客户端和服务器端代码,但我无法调用该函数。 这是输出中的编译错误

Line 31 1119: Access of possibly undefined property doSomething through a reference with static type flash.net:SharedObject.

任何建议我如何解决这个问题对as3新手。请有人帮帮我吗?

var nc:NetConnection = new NetConnection();

nc.connect("rtmfp://localhost/submitSend");

nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);

function netHandler(event:NetStatusEvent):void{
    switch(event.info.code){
        case "NetConnection.Connect.Sucess":
        trace("Connecting...");
        break;

        case "NetConnection.Connect.Failed":
        trace("Unable to connect up");
        break;

        case "NetConnection.Connect.Rejected":
        trace("Whoops");
        break;
    }
}

var so:SharedObject = SharedObject.getRemote("mySo", nc.uri, true);

so.connect(nc);

so.doSomething = function(str) {
    // Process the str object.
};

服务器端:

var so = SharedObject.get("mySo", true);
so.send("doSomething", "This is a test");

1 个答案:

答案 0 :(得分:0)

正如我在之前的评论中所说,欢迎提供您所引用文件的链接,以帮助人们帮助您...

这里已经提到了一些要点:

  • 您应该在调用connect()之前添加您的事件监听器。
  • 只有在收到NetConnection.Connect.Success事件后才会连接您的共享对象(顺便说一下,您的样本中有此名称的拼写错误)
  • 您应该将类​​实例设置为共享对象的客户端。

我不确定所有这些都能解决您的问题,但您可以尝试一下:

var nc:NetConnection = new NetConnection();

private function netHandler(event:NetStatusEvent):void
{
    switch(event.info.code)
    {
        case "NetConnection.Connect.Success":
        {
            trace("Connecting...");
            connectSharedObject();
            break;
        }
        case "NetConnection.Connect.Failed":
        {
            trace("Unable to connect up");
            break;
        }
        case "NetConnection.Connect.Rejected":
        {
            trace("Whoops");
            break;
        }
    }
}

private function connectSharedObject():void
{
    var so:SharedObject = SharedObject.getRemote("mySo", nc.uri, true);
    so.client = this;
    so.connect(nc);
}

public function doSomething(str:String):void
{
    // Process the str object.
}

nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
nc.connect("rtmfp://localhost/submitSend");