Posted by: prashantbhagwat | August 13, 2008

AIR_SWF communication

Hi,

Below is example which gives the idea how to communicate between AIR/Flex application and SWF (which is created using Flash 8).

Step 1: Create a Flex Project (Desktop application) and copy paste the following code in mxml file.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”vertical” creationComplete=”fnInit()”>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.net.LocalConnection;
import flash.events.StatusEvent;

// This variable is used to accept the connection from SWF
private var incommingConn : LocalConnection;
// This variable is used to establish the connection with SWF
private var outgoingConn : LocalConnection;

private function fnInit () : void
{
outgoingConn = new LocalConnection;
outgoingConn.addEventListener (StatusEvent.STATUS, fnOnStatus);

incommingConn = new LocalConnection ();
incommingConn.allowDomain();
incommingConn.client = this;

try
{
incommingConn.connect ("SWFtoAIR");
}
catch (err : Error)
{
Alert.show ("Connection is already in use by other SWF. " + err.message);
}
}

private function fnOnStatus (evt : StatusEvent) : void
{
switch (evt.level)
{
case 'status':
Alert.show ("LocalConnection is established.");
break;
case 'error':
Alert.show ("LocalConnection is failed.");
break;
}
}
// This method establish the connection with SWF and call the SWF method 'fnRecieveMessage'.
private function fnSendMessage (str : String) : void
{
outgoingConn.send("localhost:AIRtoSWF", "fnReceiveMessage", str);
}
// This method is called from SWF when connection is established successfully
public function fnGetMessageFromSWF (str : String) : void
{
Alert.show ("Message recieved : " + str);
}
]]>
</mx:Script>
<mx:Button id=”btnMsg” label=”Send Message” click=”fnSendMessage (’MSG from AIR’);”/>
</mx:WindowedApplication>

—————————————————————————-

Step 2: Create a Flash document file and copy paste the following code in file.


stop();
// This variable accept the incomming connection from AIR application
var incommingConn : LocalConnection;
// This vaiable establish the connection with AIR application
var outgoingConn : LocalConnection;
//
fnInit ();
//
function fnInit ()
{
outgoingConn = new LocalConnection ();
//
incommingConn = new LocalConnection ();
incommingConn.allowDomain(”*”);
incommingConn.connect(”AIRtoSWF”);
}
// This is a callback method called from AIR application when connection established successfully
incommingConn.fnReceiveMessage = function (msg:String)
{
msg_txt.text = msg;
}
// This callback method indicate that it is ready to accept the request from AIR application
incommingConn.allowDomain = function ()
{
return true;
}
// This method is called whenever SWF communicate with AIR application
outgoingConn.onStatus = function (info:Object)
{
switch (info.level)
{
case ’status’:
debug_txt.text = “Local connection is succecced.”;
break;
case ‘error’:
debug_txt.text = “Local connection fail to connect.”;
break;
}
}
// This method establish the connection with AIR and call the AIR method ‘fnGetMessageFromSWF’.
function fnSendMessage ()
{
outgoingConn.send(”app#AIR-SWF-Communication:SWFtoAIR”, “fnGetMessageFromSWF”, “Message from SWF to AIR app.”);
}

Step 3: Run Flex application and SWF and test the code.

—————————————————————————–

Small explanation about Flash code -
In Flash file I have created two textfields, debug_txt to show the connection status, and msg_txt to display the message received from AIR/Flex application.

To send the message from Flash to AIR/Flex I have written the function fnSendMessage. In fnSendMessage I have passed three parameters. The first is the domain and connection name by which communication is established, second is AIR/Flex method to be invoked, and third parameter is message that is to be sending to AIR/Flex application. This method is called on button event as on (release){ fnSendMessage();}

You can get the correct domain name by tracing outgoingConn.domain () method. Use this string in outgoingConn.send() method to establish a connection, in this example I have used “app#AIR-SWF-Communication:SWFtoAIR”.


Leave a response

You must be logged in to post a comment.

Categories