开发者

LocalConnection: communicate between flex 4.1 and flash as2

I'm trying to communicate between a flex 4.1 application to a flash action script 2 application using LocalConnection.

flash application

contains a button called btn01 and开发者_Go百科 the following code:

var a:LocalConnection = new LocalConnection();

btn01.onPress = function() {
 trace("button clicked");
 a.send("abcde","test");
}

you can see here that it sends a test command to the connection named 'abcde'.

flex application

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955"     minHeight="600" initialize="init()">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;

        private function init():void {
            var a:LocalConnection = new LocalConnection();
            a.client=this;
            a.connect("abcde");
        }

        public function test():void {
            Alert.show("test");
        }
]]>
</fx:Script>
<mx:SWFLoader source="/location/as2-flash-file.swf" />

as you can see, in the flex application i connect to LocalConnection named 'abcde' and i set the client to 'this' which means that all the public functions can be executed from the LocalConnection.

the SWFLoader element loads the as2 flash file.

whenever i click the button i do see the trace message but the function test does not get executed on the flex application. any ideas?

update

both applications sit on the same domain, on the localhost actually so no need for allowDomain usage and both applications are web based.


Documentation says AS2 and AS3 LocalConnections should communicate no problems.

Do you need to look into the allowDomain method? Do you need to put a crossdomain.xml file in place? If you do have swfs on two different domains, pay special attention to the send method documentation, because you you have to add additional info to the send method's connection name.

Are they both browser based applications? I not, look into AIR


I created the LocalConnection variable within the init() scope, so when the function ended the localconnection was destroyed. the solution is just to declare the variable outside of the init function.

public var a:LocalConnection;

private function init():void {
        a = new LocalConnection();
        a.client=this;
        a.connect("abcde");
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜