0

Adobe 文档中似乎有关于使用共享object.send(). 我正在尝试对所有客户端执行发送方法。

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

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");
4

1 回答 1

0

As said in my previous comment, a link to the document you're refering to would be welcome to help people helping you...

Here is already some points that ought to be mentionned:

  • You should add your event listeners before any call to connect().
  • You should connect your shared object only once you received the NetConnection.Connect.Success event (by the way, you have a typo in your sample on this name)
  • You should set you class instance as the client of your shared object.

I'm not sure all of this will fix your issue but you can try this:

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");
于 2013-05-10T02:38:08.203 回答