Flex通信篇——Flex和外部应用程序进行通信

彭博 发布于 2012/03/09 12:24
阅读 260
收藏 0

Flash To EXE

 

Flex端

  1. Flex创建一个Flex Project命名为Demo1,类型选Web application(runs in Flash Player)
  2. 在Demo1.mxml上添加一个按钮,并未按钮添加点击事件

    private function ButtonClick(event:MouseEvent):void

    {

        //调用外部程序SayHello方法,并传入方法参数"Hunk",输出方法返回值result

        if (!ExternalInterface.available) return ;

        var result:String = ExternalInterface.call("SayHello", "Hunk");

        trace(result);

    }

EXE端

  1. VS2008创建window应用程序项目
  2. 打开默认的窗体,向窗体添加ShockwaveFlashObject控件
  3. 向窗体添加下面代码,为ShockwaveFlashObject控件注册FlashCall事件接收Flash发送过来的请求

protected override void OnLoad(EventArgs e)

{

    if (m_Init) return;

    m_Init = true;

    string swfPath = Path.Combine(Application.StartupPath, ConfigurationManager.AppSettings["SwfPath"]);

    if (System.IO.File.Exists(swfPath))

    {

    axShockwaveFlash1.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(axShockwaveFlash1_FlashCall);

//通过配置文件加载Demo1.swf

    axShockwaveFlash1.Movie = System.IO.Path.Combine(Application.StartupPath, swfPath);

    }

}

private void axShockwaveFlash1_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)

{

    try

    {

        //当点击Demo1.swf的按钮时,该方法就会被触发,这里需要解析swf发送过来的xml请求信息

     //e.request格式

     //<invoke name="SayHello" returntype="xml"><arguments><string>Hunk</string></arguments></invoke>

 

       //根据e.request内容执行相关操作

 

       //操作处理完成后需要返回值到swf,这时需要调用ShockwaveFlashObject控件的SetReturnValue方法

       //返回Hello!给Demo1.swf

       axShockwaveFlash1.SetReturnValue("<string>Hello!</string>");

    }

    catch (Exception ex)

    {

    }

}

总结

  1. Flex调用外部应用程序方法(或flash to html)时主要使用ExternalInterface.call方法,详细了解该方法请查阅帮助。该方法会有一个比较严重的问题,当外部应用程序方法执行时间过长(大于60秒)而没有值返回时会报1502错误,所以操作时间大于60秒的方法需要另外的方式来实现,下篇会介绍如何实现异步调用方式解决该问题。
  2. 外部应用程序主要通过注册ShockwaveFlashObject控件的FlashCall事件来接收flash发送过来的请求,请求是xml格式的字符串,详细可查找flex builder的帮助了解。返回值时调用ShockwaveFlashObject控件的SetReturnValue方法。

进阶

理解了Flex To EXE的原理后就可以设计一些比较有用的方法让EXE来完成Flex所不能完成的操作(不用AIR),下面实现一个本地IO的API

package common

{

    import flash.external.ExternalInterface;

 

    public class LocalAPI

    {

        //Singleton static obj

        private static var g_Instance:LocalAPI = null;

 

        //获取LocalAPI单件实例

        public static function get Instance():LocalAPI

        {

            if (g_Instance == null)

            {

                g_Instance = new LocalAPI();

            }

            return g_Instance;

        }

 

        public function LocalAPI()

        {

            if (g_Instance != null)

                throw new Error("Singleton class. Please use Instance static filed.");

        }

 

        //写日志

        public function Log(message:String):Number

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_LOG, message);

        }

 

        //复制文件

        public function CopyFile(srcFilePath:String, destFilePath:String):Number

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_COPY_FILE, srcFilePath, destFilePath);

        }

 

        //删除文件

        public function DeleteFile(filePath:String):Number

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_DELETE_FILE, filePath);

        }

 

        //复制文件夹

        public function CopyFolder(srcFolderPath:String, destFolderPath:String):Number

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_COPY_FOLDER, srcFolderPath, destFolderPath);

        }

 

        //删除文件夹

        public function DeleteFolder(FolderPath:String):Number

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_DELETE_FOLDER, FolderPath);

        }

 

        //写文件

        public function WriteFile(filePath:String, content:String, append:Boolean):Number

        {

            if (append)

                return ExternalInterface.call(Command.FLASH_TO_APP_WRITE_FILE, filePath, content, "true");

            else

                return ExternalInterface.call(Command.FLASH_TO_APP_WRITE_FILE, filePath, content, "false");

        }

 

        //读取文件

        public function ReadFile(filePath:String):String

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_READ_FILE, filePath);

        }

 

        //获取路径的子文件夹列表

        public function GetFolderList(folderPath:String):Array

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_GET_FOLDER_LIST, folderPath);

        }

 

        //获取路径的子文件列表

        public function GetFileList(folderPath:String):Array

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_GET_FILE_LIST, folderPath);

        }

 

        //创建新的文件夹

        public function CreateDirectory(folderPath:String):Number

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_CREATE_FOLDER, folderPath);

        }

 

        //判断文件是否存在

        public function IsFileExist(path:String):Number

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_EXIST_FILE, path);

        }

 

        //判断文件夹是否存在        

        public function IsFolderExist(path:String):Number

        {

            return ExternalInterface.call(Command.FLASH_TO_APP_EXIST_FOLDER, path);

        }

    }

}

 

系列索引

Flex通信篇——Flex和外部应用程序进行通信

Flex通信篇——Flex和外部进行异步通信

Flex通信篇——Flex键盘组合键

Flex通信篇——构建企业级HTTP通信层


原文链接:http://blog.csdn.net/hunkcai/article/details/5011767
加载中
返回顶部
顶部