LocalConnection for debugging

Did you know you can communicate between two different flash applications running at the same time?  This is possible using the LocalConnection Object.  I’ll often use this for building app debuggers, so i can see the debug outside the main application.  Here is a simple example, where various events in one mxml app are broadcast to the LocalConnection, and a seperate mxml app is listening and outputs details on each event.

LC1.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" initialize="initialize()" creationComplete="sendMessage(event)" mouseDown="sendMessage(event)">
    <mx:Script>
        var myConnection;
        function initialize() {
            myConnection = new LocalConnection();
            myConnection.onStatus = mx.utils.Delegate.create(this, onStatus);
        }
        function sendMessage(event) {
            myConnection.send("receivingapp", "messagePosted", event.target + "–" + event.type);
        }
        function onStatus(result) {
            label.text=result.level == "error"?"Operation failed":"Operation succeeded";
        }
    </mx:Script>
    <mx:Label id="label" creationComplete="sendMessage(event)" mouseOver="sendMessage(event)" mouseOut="sendMessage(event)" mouseDown="sendMessage(event)"/>
</mx:Application>

LC2.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" initialize="initialize()">
    <mx:Script>
        var myConnection;
        function initialize() {
            myConnection = new LocalConnection();
            myConnection.messagePosted = mx.utils.Delegate.create(this, messagePosted);
            myConnection.connect("receivingapp");
        }
        function messagePosted(message) {
            messageList.text+=message+"n";
        }
    </mx:Script>
    <mx:TextArea id="messageList" width="300" height="300"/>
 <mx:Button label="reset" click="messageList.text=”"/>
</mx:Application>

You can find more details about this in Christophe’s blog, or examples using this in Flash at EML or the Team Macromedia blogs.

There are no comments.

Leave a Reply