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”.

Posted by: prashantbhagwat | June 5, 2008

Multi Page printing in Flash MX using Javascript

Hi,

Using Flash MX, we are not able to print the data in multiple page. If we try to print the movieclip which has text to print and this movieclip exceeds the page limit, Flash print function compress the data into single page.

To over come this problem, we have another way to print the data using Javascript.

Lets see how to do it -

Step 1: Create a flash file from where we need to print the multipage data. Which have input text field and print button. Print button will call fscommand as fscommand (“printContent”);

Step 2: Publish the Flash file as Flash with FSCommand.

Step 3: Add switch statement in published HTML which will handle the FSCommand send by Flash.

        switch (command)
        {
           case “printContent”:
                   var str = “<html><body>”
                  str = str + args;
                  str = str + “</body></html>”
                  parent.forPrintFrame.document.write(str);
                 fnPrintContent ();
                 break;
       }

And some more script as -

var t;
function fnPrintContent ()
{
 t = setTimeout(“parent.fnPrint();”,1000);
}

fnPrintContent will call another function, fnPrint (which is present in parent frame) after 1 second.

Step 4: Create a empty HTML file as PrintData.html

Step 5: Now create index.html which has 2 frames like -

<FRAME SRC=”flashFile.html” NAME=”content” NORESIZE  FRAMEBORDER=”NO”>
 <FRAME SRC=”PrintData.html” id=”printFrm” NAME=”forPrintFrame” NORESIZE FRAMEBORDER=”NO”>

First frame has published flash file and second frame will have empty html file.

Now add following function in index.html file -

function fnPrint ()
{
 window.clearTimeout(content.t);
 window.forPrintFrame.focus ();
 window.print ();
}

Step 6: Now run the index.html file, write some data in flash file and click the Print button.

 

Posted by: prashantbhagwat | August 13, 2007

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Categories