As our application grows larger and larger, it needs us to separate the application to small different applications. However, we got problems when the small different applications in different domains share same codes. By default, flash player will separate the applications come from different domain into different package, so that one application can run without interrupting others. Before the topic, we need to know something about the flash player security settings.
First, we need to know what's Sandbox Type. Sandbox Type is a way to separate applications in different domains. It depends on its display url and compiler options. See the picture below.
Different applications with different Sandbox Type won't be able to share the same code. Since most of time the applications are running on the web, in fact there is no need to worry about such a problem.
Now, support we have two applications located in http://domainA.com/a.swf and http://domianB.com/b.swf . The b.swf is going to be loaded in a.swf, but the b.swf needs some codes that run in a.swf. The connection likes the picture below.
Since the a.swf want to access the properties and methods in b.swf, so we need to add "flash.system.Security.allowDomain( '*' ) //as3.0; System.security.allowDomain('*') // as2.0" in b.swf.
Cross Domain Applications Share Same Codes in AS3.0
In as3.0, there are two ways to separate different applications in different domains. The first is ApplicationDomain, and the second is SecurityDomain. So if we want to share codes between http://domainA.com/a.swf and http://domainB.com/b.swf , we need to load b.swf into a.swf with the same ApplicationDomain and SecurityDomain. See the codes below. It's quite simple.
var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, flash.system.SecurityDomain.currentDomain);
var request:URLRequest = new URLRequest(url);
loader.load(request, ldrContext);
Cross Domain Applications Share Same Codes in AS2.0
In as2.0, there is no conception about ApplicationDomain and SecurityDomain. However, the applications in different domains are strictly separated into different domains. We still haven't found any way to let the applications running in the same domain. However, there do exist some other ways to sovle such a problem.
Suppose Application.A class and Application.B class are defined in http://domainA.com/a.swf , http://domainB.com/b.swf have to share them in a.swf. Before we load the b.swf into a.swf, we need to load Application.A class and Application.B class in to b.swf domain, so that b.swf will be able to use them in its own domain.
Since the classes are defined before any flash events under control in VIM, we get no way to share the codes after b.swf is loaded into a.swf, so we need a b_proxy.swf that is in the same domain with b.swf. First, a.swf loads b_proxy.swf. Second, b_proxy.swf defines the shared classes in its domain. Third, a.swf loads b.swf to after b_proxy.swf was loaded. See the picture below.
After b_proxy.swf was loaded into a.swf. We need to add some codes in b_proxy.swf. See the codes below:
System.security.allowDomain("*");
function setApplication(application)
{
_global.Application = application;
}
Then add some codes in a.swf after the b_proxy.swf was loaded into a.swf.
function onLoadInit(b_proxy:MovieClip)
{
b_proxy.setApplication = _global.Appliation;
}
Now, you can load b.swf into a.swf.
Wonderful~ b.swf can share the codes with a.swf right now! But wait a minute, do you find something strange? Yeah, Some properties defined in a.swf won't work in b.swf . Why ? Because they are still running in different domains. b.swf will initialize all the instances again releated to its application, but won't share all the properties that are running in a.swf. So we need to share properties that are predefined in a.swf. All these properties can be stored in a instance called environment which is responsible for accepting parameters from outside, adding events listeners and dispatching events.
After the b.swf was loaded in a.swf, all you need to do is let both of them share the same environment. See the codes below.
function onLoadinit(b:Movieclip)
{
b.environment = a.environment;
}
Great, it works again. ^_^
Conclusion-
Cross domain applications can share the same codes in AS3.0 and it's easy to make it.
-
It seems as if there is no way to share the same codes in AS2.0, but we can use proxy.swf and environment to let them run as if in the same domain.
-
Never forget to add system.security.allowDomain("*") in b.swf.