12 Replies Latest reply on Nov 16, 2010 8:41 AM by Green Goby

    Getting access to swf bridge in loaded sub application

    Green Goby

      In my appplication that has been loaded, I want to get a handle to the swf bridge so I can communicate with the parent application.  The parent application, via the SWFLoader has direct access to the bridge, but what is the best / most reliable way to get access to the bridge in the loaded application?

       

      I see in some of the flex sdk code they are casting the SystemManager to a DisplayObject and using loaderInfo.sharedEvents.  That just doesn't seem right.

       

      Thanks in advance for any help.

       

      Irv

        • 2. Re: Getting access to swf bridge in loaded sub application
          Jean Demonceau

          Your loaded application, (in the main file !) must dispatch events like this :

           

          var event:CustomEvent = new CustomEvent();

          loaderInfo.sharedEvents.dispatchEvent(event);

           

          If you want to dispatch events from a subcomponent of your loaded application, do it via a mainFile reference.

           

          var event:CustomEvent = new CustomEvent();

          myMain.loaderInfo.sharedEvents.dispatchEvent(event);

           

           

          The application that loads the other one must listen for events like this :

           

          loader.source = "subApplication.swf";

          loader.addEventListener(Event.COMPLETE, handlerSubApplicationComplete);

           

          //The sub application is fully loaded, now I can listen for custom events
          private function handlerSubApplicationComplete(e:Event):void
          {
                    var subApplication:EventDispatcher = (e.target.mx_internal::contentHolder as Loader).contentLoaderInfo.sharedEvents;

                    subApplication.addEventListener("myCustomEvent",handlerCustomEventReceived, false, 0, true);
          }

          • 3. Re: Getting access to swf bridge in loaded sub application
            Green Goby Level 1

            Thank you for your answers.  My question is really if I am not in a displayObject of the loaded application, how do I get a handle to the loaderInfo?  What is the best and most reliable way to do that.

             

            I know in the loading application I can get a handle via SWFLoader.loaderInfo.  But in the loaded application, (and not in a display object) how does one get a handle to the loaderInfo.

             

            Thanks in advance.

             

            Irv

            • 4. Re: Getting access to swf bridge in loaded sub application
              Jean Demonceau Level 1

              I'm used to make a Singleton in all applications I write and to set the reference of the main in it.

              So, anywhere in my applications, I can write : mySingleton.main.loaderInfo....

               

              Here is an example of singleton class

               

              package
              {

                   import flash.events.EventDispatcher;

               

                  public class MySingleton extends EventDispatcher
                  {

                        // Singleton
                      private static var _instance:MySingleton ;  

               

                      public function MySingleton ()
                      {
                          if(_instance != null)
                              throw new Error("Singleton can only be accessed through MySingleton.getInstance()");
                          else
                          {
                              //Instiante the instance
                              _instance = this;   
                          }   
                      }
                     
                      //Singleton
                      public static function getInstance():MySingleton
                      {
                          if(_instance == null) _instance = new MySingleton();
                          return _instance;
                      }

               

                      [Bindable]
                      public function get main():MyApp
                      {
                          return getInstance()._main;
                      }
                      public function set main(value:MyApp):void
                      {
                          getInstance()._main = value;
                      }

                   }

              }

               

              In your MyApp, in the creationComplete handler, you write :

              MySingleton.getInstance().main = this;

               

               

              And anywhere in your application you dispatch your events like this :

              MySingleton.getInstance().main.loaderInfo.....

              • 5. Re: Getting access to swf bridge in loaded sub application
                Flex harUI Adobe Employee

                In the general sense, you can't find out. In Flex, you can try using

                SystemManager.getSWFRoot to get a SystemManager that loaded the class for

                the non-display object.  I would recommend passing in a reference to the

                SystemManager to your non-display object though.

                • 6. Re: Getting access to swf bridge in loaded sub application
                  Green Goby Level 1

                  In looking through the code, what about this?

                   

                  Singleton.getClass("mx.managers::IMarshalSystemManager");

                   

                  IMarshalSystemManager then lets me call the method useSWFBridge, then let's me get a handle to swfBridgeGroup, which then let's me get a handle to the parentBridge.

                   

                  It seems this is reliable and is the way the internal classes get their work done.

                   

                  Does anyone see a problem with this?

                   

                  Thanks,

                   

                  Irv

                  • 7. Re: Getting access to swf bridge in loaded sub application
                    Flex harUI Adobe Employee

                    The parentBridge should be:

                    DisplayObject(systemManager).loaderInfo.sharedEvents;

                    • 8. Re: Getting access to swf bridge in loaded sub application
                      Green Goby Level 1

                      Thank you, that worked great.  For what it is worth, doing a Singleton.getInstance on the IMarshalSystemManager actually throws a runtime error.  It can find the class, but then doing getInstance on that class, IMarshalSystemManager doesn't have that method, which throws a runtime error.  Seems like this violates the Singleton api, but I was able to use the sharedEvents on the main application to communicate.

                       

                      My question now is if I send an event, and I want the receiver of the event to be able to set some data on the event for the original caller to get, that doesn't seem to be working.  I am trying to set a Boolean and a String property, but the original sender of the message isn't getting the values.

                       

                      In the sender, the code looks like:

                       

                                  var bridgeEvent:BridgeEvent = new BridgeEvent( BridgeEvent.SUB_APPLICATION_INITIALIZED_TYPE, null, new Date().milliseconds + "" );

                                  bridgeEvent.isRequest = true;

                                  bridge.dispatchEvent( bridgeEvent );

                                  // after dispatching get the response data

                                  this._idForBridgeEvents = bridgeEvent.responseData as String;

                       

                      In the receiver it does:

                       

                                  var eventObject:Object = event;

                                  eventObject.isHandled = true;

                                  eventObject.responseData = this._id;

                      I have verified that the receiver is putting in the right data, but the sender is never seeing the Boolean or String.
                      Is there something I can do here?  I thought this was supposed to be the general approach.  I can always send back another event with response data, but I'd like to avoid that here.
                      Thanks,
                      Irv

                      • 9. Re: Getting access to swf bridge in loaded sub application
                        Flex harUI Adobe Employee

                        Maybe the event is getting cloned or marshalled unexpectedly?  Use the

                        debugger to verify you are working with the right instance.

                        • 10. Re: Getting access to swf bridge in loaded sub application
                          Green Goby Level 1

                          I am literally only doing what you see in the code.  This is supposed to work across the bridge right?  I went looking through the source code for flex and I could only find examples of sending data, not using "return" types as I am here.

                           

                          Is there anything else that can make it get cloned?  This is going across different ApplicationDomains.

                           

                          Irv

                          • 11. Re: Getting access to swf bridge in loaded sub application
                            Flex harUI Adobe Employee

                            If it gets re-dispatched that will clone it.  Set a breakpoint at the

                            dispatchEvent line.  Examine the event. The debugger should show you that is

                            is object @2398231 or whatever.  Set a breakpoint in the handler, see if the

                            event is the same object.  After setting it, use "step into" until you get

                            back to dispatchEvent to make sure no other handler stomped those

                            properties.

                            • 12. Re: Getting access to swf bridge in loaded sub application
                              Green Goby Level 1

                              I probably should create a new thread, but for anyone else who has to do all this stuff, this is a nice thread with lots of answers. 

                               

                              Our application is like Mosaic but just for applications that the company I work for produces.  (So a limited set of like 10 applications)  I am trying to decide if I should call unloadAndStop on the SWFLoader when I go to another app.  The business folks do want the app to stop, but I am wondering if there is another way to stop the app, but if they come back "reload" it without having to reload the SWF again.  I think from looking at the Loader and SWFLoader code it knows if it has loaded the SWF file before,  but I am wondering about side affects at this point.  Is it safer to unload the SWF?  Any memory leaks or gotchas with doing that? 

                               

                              Thanks,

                               

                              Irv