Search Flex Components Free

Custom Search

December 27, 2007

communication between SWF files in Flex

Creat a New mxml file:

localConnectionReciever.mxml

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”
http://www.blogger.com/ ” creationComplete=”init()”>
<mx:Script>
<![CDATA[
import flash.net.LocalConnection;
import mx.controls.Alert;
private var conn:LocalConnection;
public function init():void
{
conn = new LocalConnection();
conn.client = this;
try {
conn.connect(”myConnection”);
}
catch (error:ArgumentError) {
Alert.show(”Can't connect…the connection name is already being used by another SWF”);
}
}
public function lcHandler(msg:String):void {
output.text=msg + “\n”;
}

]]>
</mx:Script>
<mx:TextArea id=”output” width=”221″ height=”221″ wordWrap=”true”/>
</mx:Application>

localConnectionSender.mxml

<?xml version=”1.0″?>
<mx:Application xmlns:mx=”
http://www.blogger.com/ ” height=”400″
creationComplete=”init()”>
<mx:Script>
<![CDATA[
import flash.events.MouseEvent;
import flash.net.LocalConnection;
import flash.events.StatusEvent;
import mx.controls.Alert;
private var conn:LocalConnection;
public function init():void {
sendBtn.addEventListener(MouseEvent.CLICK, sendMessage);
conn = new LocalConnection();
conn.addEventListener(StatusEvent.STATUS, onStatus);
}
private function sendMessage(event:MouseEvent):void {
conn.send(”myConnection”, “lcHandler”, message.text);
}
private function onStatus(event:StatusEvent):void {
switch (event.level) {
case “status”:
Alert.show(”LocalConnection.send() succeeded”);
break;
case “error”:
Alert.show(”LocalConnection.send() failed”);
break;
}
}
]]>
</mx:Script>
<mx:HBox>
<mx:Text id=”messageLabel” width=”93″ height=”35″ text=”Text to send:” enabled=”false”/>
<mx:TextInput id=”message” width=”317″ height=”65″ />
<mx:Button id=”sendBtn” label=”Send” />
</mx:HBox>
</mx:Application>

Related Flex Tutorials