:http://hnjnd.com/ 自己创业做LED环保灯,致力于公益企业,企业公益实践。
记:据官方API描述是可以远程加载Module的,官方air文档中明显避而不谈,在网上淘了几天也没淘个所以然,源代码也看得人迷糊。还有怪异的安全沙箱,调试时variables栏中和java调试时的迥异,让我焦头烂额。打算自己写个module管理,今天做个预备。
2、安全沙箱:
可以在三个地方设置:
1)mmc.cfd操作系统用户控制的文件。我在我机器上依照官方说明按图索骥,no,没有这个文件,只好自己建了个,有没用天知道。官方参考:http://livedocs.adobe.com/flash/9.0_cn/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000350.html
3)程序员控制
Security.allowDomain("www.example.com")
4)策略文件:放web服务器的文件,里面列出了谁谁能加载服务器上的swf等文件,见鬼的是我是air的,类似传统的CS中的客户端,我又没放web服务器上,咋知道自己是谁?只好用“*”号。谁清楚,请指教,谢了。
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
5)遇到的问题:
- 将模块文件直接下载写到File.applicationDirectory.nativePath,报安全沙箱错误,网上逛了圈,都说不行,后来发现是直接使用不行,但是能间接使用,代码如下
-
var file:File = File.applicationStorageDirectory.resolvePath(File.applicationDirectory.nativePath+"/test.xml");
6)远程下载源代码:原理以二进制流从服务器下载模块swf,写入本地swf,然后用ModuleManager管理即可:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[//长沙飞龙环保:http://hnjnd.com/
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.net.*;
import flash.utils.*;
import mx.controls.Alert;
public var ldr:URLLoader;
protected function download_clickHandler(event:MouseEvent):void
{
var request:URLRequest = new URLRequest("http://localhost:8080/fairlooong/Mod4.swf"); //长沙飞龙环保http://hnjnd.com/
var fileRef:FileReference = new FileReference();
try{
fileRef.download(request);
}catch(error:Error){
trace("Unable to download file.");
}
}
protected function writeFile_clickHandler(event:MouseEvent):void
{
var testXML:XML =<mousebomb><site>www.mousebomb.org</site><blog>www.flashj.cn</blog></mousebomb>;
Alert.show(Security.sandboxType);
var rootDirs:Array = File.getRootDirectories();
for (var i:uint = 0; i < rootDirs.length; i++) {
trace(rootDirs[i].nativePath);
}
trace(File.applicationStorageDirectory.url);
trace(File.desktopDirectory.url);
trace(File.documentsDirectory.url);
trace(File.documentsDirectory.url);
trace(File.applicationDirectory.nativePath );
var file:File = File.applicationStorageDirectory.resolvePath(File.applicationDirectory.nativePath+"/test.xml");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
var outputString:String = '<?xml version="1.0" encoding="utf-8"?>';
outputString += testXML.toXMLString();
fileStream.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandle)
fileStream.writeUTFBytes(outputString);
fileStream.close();
}
private function ioErrorHandle(event:IOErrorEvent):void{
trace("IO error");
}
protected function writeMySWF():void{
ldr = new URLLoader();
var url:String = "http://localhost:8080/fairlooong/Mod4.swf";
var urlReq:URLRequest = new URLRequest(url);
ldr.dataFormat=URLLoaderDataFormat.BINARY;
ldr.addEventListener(Event.COMPLETE,completeHander);
ldr.addEventListener(IOErrorEvent.IO_ERROR, ioErroHandler);
ldr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
ldr.load(urlReq);
}
private function ioErroHandler(event:IOErrorEvent):void{
Alert.show(event.text);
}
private function securityErrorHandler(event:SecurityErrorEvent):void{
Alert.show(event.text);
}
private function completeHander(event:Event):void{
var bytes:ByteArray = ldr.data as ByteArray;
var myFile:File = File.documentsDirectory.resolvePath(File.applicationDirectory.nativePath+"/test1.swf");
var myFileStream:FileStream = new FileStream();
myFileStream.open(myFile, FileMode.WRITE);
myFileStream.writeBytes(bytes, 0, bytes.length);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Panel height="83" width="50%" x="143" y="0">
<mx:Button label="开始下载" id="download" click="download_clickHandler(event)" />
<mx:Button label="开始写文件" id="writeFile" click="writeFile_clickHandler(event)" />
<mx:Button label="开始写SWF" id="writeSWF" click="writeMySWF()" />
</mx:Panel>
</s:WindowedApplication>
7)应该是可以直接加载至内存中然后去访问的,待下周末研究
在网上看到一句话:actionscript是个好东西,可是flex不是个好东西