▼Adobe AIRメモ▼
AIRアプリケーションとFlashコンテンツの連携
AIRアプリケーションとFlashコンテンツの連携を行うプログラムを作成する。
AIRアプリケーションのソースコード
LocalConnectionAIREx.as package { import flash.display.*; import flash.events.*; import flash.net.*; import flash.text.*; import flash.ui.*; //AIRアプリケーションとFlashコンテンツの連携(AIR) public class LocalConnectionAIREx extends Sprite { private const APP_ID:String=//アプリケーションID "localhost:myConnection"; private var tfView:TextField; private var tfSend:TextField; private var lc:LocalConnection; //コンストラクタ public function LocalConnectionAIREx() { //テキストフィールドの生成 tfView=addTextField(10,10,220,190); tfSend=addTextField(10,210,220,20); tfSend.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown); //ローカルコネクションの生成 lc=new LocalConnection(); lc.addEventListener(StatusEvent.STATUS,onStatus); //ローカルコネクションの受信 lc.allowDomain("*"); lc.addEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsyncError); lc.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError); try { lc.client=this; lc.connect("myConnection"); } catch (e:ArgumentError) { addText("接続名が既に使用中"); } } //テキストフィールドの追加 private function addTextField(x:int,y:int,w:uint,h:uint):TextField { var textField:TextField=new TextField(); addChild(textField); textField.x=x; textField.y=y; textField.width=w; textField.height=h; textField.text=""; textField.selectable=true; textField.background=true; textField.backgroundColor=0xFFFFFF; textField.type=TextFieldType.INPUT; return textField; } //テキストの追加 public function addText(text:String):void { tfView.text=text+"\n"+tfView.text; } //ステータスイベントの処理 private function onStatus(evt:StatusEvent):void { switch(evt.level) { case "status": break; case "error": addText("エラー"); break; case "warning": addText("警告"); break; } } //同期エラーイベントの処理 private function onAsyncError(evt:AsyncErrorEvent):void { addText("同期エラー>"+evt.text); } //セキュリティーエラーイベントの処理 private function onSecurityError(evt:SecurityErrorEvent):void { addText("セキュリティエラー>"+evt.text); } //キーダウンイベントの処理 private function onKeyDown(evt:KeyboardEvent):void { if (evt.keyCode==Keyboard.ENTER) { if (tfSend.text.length==0) return; //ローカルコネクションの送信 lc.send(APP_ID,"addText",tfSend.text); addText(tfSend.text); tfSend.text=""; } } } }
Flashコンテンツのソースコード
LocalConnectionSWFEx.as package { import flash.display.*; import flash.events.*; import flash.net.*; import flash.text.*; import flash.ui.*; //AIRアプリケーションとFlashコンテンツの連携(SWF) public class LocalConnectionSWFEx extends Sprite { private const APP_ID:String=//アプリケーションID "app#net.npaka.LocalConnectionAIREx:myConnection"; private var tfView:TextField; private var tfSend:TextField; private var lc:LocalConnection; //コンストラクタ public function LocalConnectionSWFEx() { //テキストフィールドの生成 tfView=addTextField(10,10,220,190); tfSend=addTextField(10,210,220,20); tfSend.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown); //ローカルコネクションの生成 lc=new LocalConnection(); lc.addEventListener(StatusEvent.STATUS,onStatus); //ローカルコネクションの受信 lc.allowDomain("*"); lc.addEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsyncError); lc.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError); try { lc.client=this; lc.connect("myConnection"); } catch (e:ArgumentError) { addText("接続名が既に使用中"); } } //テキストフィールドの追加 private function addTextField(x:int,y:int,w:uint,h:uint):TextField { var textField:TextField=new TextField(); addChild(textField); textField.x=x; textField.y=y; textField.width=w; textField.height=h; textField.text=""; textField.selectable=true; textField.background=true; textField.backgroundColor=0xFFFFFF; textField.type=TextFieldType.INPUT; return textField; } //テキストの追加 public function addText(text:String):void { tfView.text=text+"\n"+tfView.text; } //ステータスイベントの処理 private function onStatus(evt:StatusEvent):void { switch(evt.level) { case "status": break; case "error": addText("エラー"); break; case "warning": addText("警告"); break; } } //同期エラーイベントの処理 private function onAsyncError(evt:AsyncErrorEvent):void { addText("同期エラー>"+evt.text); } //セキュリティーエラーイベントの処理 private function onSecurityError(evt:SecurityErrorEvent):void { addText("セキュリティエラー>"+evt.text); } //キーダウンイベントの処理 private function onKeyDown(evt:KeyboardEvent):void { if (evt.keyCode==Keyboard.ENTER) { if (tfSend.text.length==0) return; //ローカルコネクションの送信 lc.send(APP_ID,"addText",tfSend.text); addText(tfSend.text); tfSend.text=""; } } } }
−戻る−