10 Replies Latest reply on Apr 11, 2008 4:46 AM by takopus

    localConnection

    zacko_2
      Can you use localConnection to talk to an SWF that is running in your AIR app? If the app you are building is with HTML and JS, and there is a SWF file on the page, can AIR talk to that SWF using localConnection?

      Please help, I can't seem to find very good documentation on this! If anyone has any good links, feel free to post.

      Thanks
        • 1. Re: localConnection
          Oliver Goldman Adobe Employee
          Yes, you can use LocalConnection. See http://livedocs.adobe.com/flex/3/html/Scripting_and_Communications_1.html.

          Oliver Goldman | Adobe AIR Engineering

          • 2. Re: localConnection
            zacko_2 Level 1
            Oliver,
            Thank you for the link. However, I am getting a 404 error when trying to view this page. Do you have a different link? Also, this seems to be a Flex solution, I am developing in HTML and JS as well as Flash for a small piece of the app, or does that not matter?

            Thanks again for your response!
            • 3. Re: localConnection
              zacko_2 Level 1
              Okay, so here is he code I am working with.
              In the SWF file:
              ////////////////////////////////////////////////////////////////////////////////
              import flash.net.LocalConnection;

              var lc:LocalConnection = new LocalConnection();
              lc.allowDomain("*");
              var clientObj:Object = new Object();
              clientObj.testMsg = function(msg){
              testObj.alpha = 0;
              }
              lc.client = clientObj;
              lc.connect("myConnection");
              ////////////////////////////////////////////////////////////////////////////////

              And this is the code in the AIR app (HTML and JS):

              ////////////////////////////////////////////////////////////////////////////////
              function tryThis(){
              var sender = new air.LocalConnection();
              sender.air.Security.allowDomain("*");
              sender.air.LocalConnection.send("myConnection","testMsg","hello");
              }
              ////////////////////////////////////////////////////////////////////////////////

              I then call it via a button using onClick. I am getting this error:
              "TypeError: Undefined value"

              What am I missing here? I can get the localConnection to work between teo SWFs running on the desktop, but when I put the SWF in my HTML and attempt to talk to it via JS in AIR I get an error msg.

              Thanks.
              • 4. Re: localConnection
                Oliver Goldman Adobe Employee
                Yes, you can use LocalConnection from HTML apps. Full AIR documentation is at http://www.adobe.com/support/documentation/en/air/.

                Oliver Goldman | Adobe AIR Engineering

                • 5. Re: localConnection
                  zacko_2 Level 1
                  A Tutorial or an example would be nice. I have looked at all these docs and can't seem to get it to work. Half the time I get an error:
                  'Error #2044: Unhandled StatusEvent:. level=error, code='

                  or it can't find an object. I can get LocalConnection to work if it is called from an SWF outside of my AIR app, but within the app it doesn't seem to work. I am starting to think that its a permission issue via the security sandbox. I am not even sure where to begin with that. Again, if you could point me to a tutorial rather than your documentation, that would be very helpful. I know I am not the only one having this issue.

                  Thanks for your response!
                  • 6. Re: localConnection
                    ctzn99 Level 1
                    put an underscore infront of your connection name

                    browser:
                    lc.connect("_myConnection");

                    air
                    sender.air.LocalConnection.send("_myConnection","testMsg","hello");

                    by using the underscore it prevents the flash player or air runtime from doing some funky domain name prepending to the real name of your connection.
                    • 7. Re: localConnection
                      zacko_2 Level 1
                      Thanks, I will give this a try! I hope this works, because my backup plan is to use SWLIVECONNECT and that offers very limited communication to an SWF instance via JS.

                      Thanks. I will post results.
                      • 8. Re: localConnection
                        zacko_2 Level 1
                        Okay, ctzn99, you are the man! Adding the underscore to the connection name is the solution. It has to be in both the Flash file as well as in AIR. So, here is the solution/sample code for anyone trying to do this.

                        The Flash file:
                        import flash.net.LocalConnection;

                        var lc:LocalConnection = new LocalConnection();
                        lc.allowDomain("*");
                        var clientObj:Object = new Object();
                        clientObj.testMsg = function(msg){
                        block.alpha = 0;
                        }
                        lc.client = clientObj;
                        lc.connect("_myConnection");


                        And the JS code for inside the AIR html file:

                        var sender = new air.LocalConnection();
                        sender.allowDomain("*");
                        function SendDataToFlashMovie(){
                        sender.send("_myConnection","testMsg","hello world");
                        }


                        If this works, and it does, it will set the alpha of the test object on the stage of the Flash movie to 0. Thanks so much again to ctzn99 for this little bit of help! And thanks to Adobe for their support and input!

                        Thanks,.
                        • 9. Re: localConnection
                          ctzn99 Level 1
                          Hey no prob. Glad I could help, the same thing fooled me for a while until I really took to the time to figure out what was going on by watching things in the flexbuilder debugger. Once I could see what was going on I could then narrow down my search in the Adobe docs to figure out why and what the solution was.
                          • 10. localConnection
                            takopus Level 1
                            Problem solved. Description below.

                            Are you sure underscope will help you?

                            There's a problem i'm facing now: i have an AIR app and some plain SWF, and they're trying to talk to each other. Both connection names starting with underscope and allowDomain set to *.
                            When i'm launching AIR application from Flash CS3 (it's not a Flex-AIR app) - it works fine. But when it launched from AIR framework, my AIR app can "hear" an SWF, but SWF can't hear an AIR app.
                            I mean, i can't send data from AIR app to local SWF, but i can from SWF to AIR app.

                            Tried all. Nothing helps. Source is here:
                            http://takopus.ru/other/TestAir.as.txt
                            http://takopus.ru/other/TestLocal.as.txt
                            first one documented a bit, second one almost identical to the first one; both simple and clean - i made them to assure of this problem.

                            Can you confirm it or maybe even help me?..

                            Problem solved.
                            Good people helped me to find solution. Problem was in "Local Playback Security" settings, in Publish Settings of a local SWF file (not an AIR app!). There was "Access Local Files only", while should be "Access Network Files only".
                            Thank you anyway!