3 Replies Latest reply on May 21, 2010 2:51 PM by flexchief

    Air to Air LocalConnection error

    flexchief Level 1

      I am unable to send and receive messages using local connection in air 1.5 but the exact same code works fine when run as a Flex app in the browser. Can someone please tell me where I am going wrong?

       

      Here is my code:

       

      Air App 1

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initConn()">
          <mx:Script>
              <![CDATA[
              import flash.net.LocalConnection;

       

              private var conn:LocalConnection;

       

              private function initConn():void
              {
                  btnSend.addEventListener(MouseEvent.CLICK, sendMessage);
                  conn = new LocalConnection();
                  conn.addEventListener(StatusEvent.STATUS, onStatus);
              }

       

              private function sendMessage(event:MouseEvent):void
              {
                  conn.send("taskConnection", "localconnectionHandler", inputTask.text);
              }

       

              private function onStatus(event:StatusEvent):void
              {
                  switch (event.level)
                  {
                      case "status":
                          labelStatus.text = "LocalConnection.send() succeeded";
                          break;
                      case "error":
                          labelStatus.text = "LocalConnection.send() failed";
                          break;
                  }
              }
              ]]>
          </mx:Script>
          <mx:Panel horizontalCenter="0" verticalCenter="0">
              <mx:Form width="100%" height="100%" horizontalCenter="0" verticalCenter="0">
                  <mx:FormItem label="Enter Task">
                      <mx:TextInput id="inputTask"/>
                  </mx:FormItem>
                  <mx:FormItem label="Send Task ">
                      <mx:Button id="btnSend" label="Send"/>
                  </mx:FormItem>
                  <mx:ControlBar>
                      <mx:Label id="labelStatus" text=""/>
                  </mx:ControlBar>
              </mx:Form>
          </mx:Panel>
      </mx:WindowedApplication>

       

       

      Air App 2

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="InitConn()">
              <mx:Script>
              <![CDATA[
                  import flash.net.LocalConnection;

       

                  private var conn:LocalConnection;

       

                  private function InitConn():void
                  {
                      conn = new LocalConnection();
                      conn.client = this;
                      try
                      {
                          conn.connect("taskConnection");
                      }
                      catch (error:ArgumentError)
                      {
                          trace("Can't connect.");
                      }
                  }

       

                  public function localconnectionHandler(msg:String):void
                  {
                      textareaTasks.text= textareaTasks.text + msg + "\n";
                  }

       

                  private function clearTasks(event:MouseEvent):void
                  {
                      textareaTasks.text="";
                  }

       

              ]]>
          </mx:Script>
          <mx:Panel  horizontalCenter="0"
              verticalCenter="0.5"
              verticalGap="15"
              paddingLeft="20" paddingRight="20" paddingBottom="20" paddingTop="20"
              height="300" width="500">
              <mx:Label text="Your tasks are..."/>
              <mx:TextArea id="textareaTasks"
                  top="20" left="20" right="20" bottom="20"
                  width="100%" height="100%"/>
              <mx:HBox>
                  <mx:Button id="btnClearTasks" click="clearTasks(event)" label="Clear Tasks"/>
              </mx:HBox>
          </mx:Panel>
      </mx:WindowedApplication>

        • 1. Re: Air to Air LocalConnection error
          Catalin Anastasoaie Adobe Employee

          The communication between the two air applications is not working because they are in different domains. In order to communicate you must explicitly tell your applications which domains are allowed to communicate with. For this you will have to make the following adjustments to your code:

           

          This code assumes that your apps are named LocalConnAir1 and LocalConnAir2

           

          For app1 you have to modify the sendMessage() function and tell it explicitly to send the the message to the connection named taskConnection from   the app#LocalConnAir1 domain:

           

                  private function sendMessage(event:MouseEvent):void

                  {

                      conn.send("app#LocalConnAir2:taskConnection", "localconnectionHandler", inputTask.text);

                  }

           

          For app2 you have to modify the initConn() function and incoming messages from the app#LocalConnAir1 domain:

           

                    private function InitConn():void

                    {

                          conn = new LocalConnection();

                          Alert.show(conn.domain);

                          conn.allowDomain("app#LocalConnAir1");

                          conn.client = this;

                          try

                          {

                              conn.connect("taskConnection");

                          }

                          catch (error:ArgumentError)

                          {

                              trace("Can't connect.");

                          }

                      }

           

          Best Regards,

          -Catalin

          • 2. Re: Air to Air LocalConnection error
            flexchief Level 1

            Brilliant! That works perfectly!

             

            I couldn't figure out where I was going wrong but that makes perfect sense .

             

            Thanks so much again Catalin you're a genius!

             

            Regards

            Adam

            Flexchief

            • 3. Re: Air to Air LocalConnection error
              flexchief Level 1

              Could someone also please tell me how to send a message back from app2 to app1? I have tried it but i am getting Error #2044: Unhandled StatusEvent:. level=error, code=

               

              Thanks

              Flexchief