AS3 XMLSocket适用于WIN EXE,但不适用于SWF

时间:2011-11-22 21:36:51

标签: actionscript-3 sockets flash

我正在尝试在FlashAS3中为我用socketjs编写的套接字服务器创建一个套接字客户端。我还有一个与之通信的移动应用客户端。

在AS3中,我编写了一些测试代码,在我发布和运行Windows EXE文件时可以很好地工作,但是当发布到SWF并在浏览器中打开它时,它不会连接到服务器。即便是所有“进口”都无济于事。守则:

import flash.system.Security;
import flash.net.XMLSocket;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.DataEvent;

var xmlSocket:XMLSocket = new XMLSocket();
xmlSocket.connect("myserverhost", 88);

xmlSocket.addEventListener(DataEvent.DATA, onIncomingData);
send_btn.addEventListener(MouseEvent.CLICK, clickHandler);
disconnect_btn.addEventListener(MouseEvent.CLICK, disconnectHandler);

function clickHandler(event:MouseEvent):void
{
    xmlSocket.send(input_txt.text);
    input_txt.text = "";
}

function disconnectHandler(event:MouseEvent):void
{
    xmlSocket.close();
    send_btn.enabled = false;
}

function onIncomingData(event:DataEvent):void
{
    var items:Object = JSON.decode(event.data);
    for (var index in items) {
        trace(index+" => "+items[index]);
    }

    if(items['hello']){
        output_txt.text += items['hello'] + "\n";
    }
    else if(items['description']){
        output_txt.text += items['description'] + "\n";
    } else {
        output_txt.text += event.data + "\n";
    }


    output_txt.verticalScrollPosition = output_txt.maxVerticalScrollPosition;
}

我一直在谷歌搜索并发现建议制作一个crossdomain.xml文件,但它不起作用,就是这样:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-access-from domain="*" to-ports="*" secure="false"/>
</cross-domain-policy>

2 个答案:

答案 0 :(得分:1)

尝试在Security.allowDonain('*');之前调用var xmlSocket:XMLSocket = new XMLSocket();

答案 1 :(得分:0)

寻找Security.loadPolicyFile

特殊语法允许直接从XMLSocket服务器检索策略文件。 例如: System.security.loadPolicyFile(的 “xmlsocket://adobe.com:414”);