So i have several SWFs in this project and i need to find a way to ensure that only a single instance of one of these SWFs can load.
For example, you cannot open 2 different application SWFs in 2 different browsers, or tabs on the same computer at the same time.
LocalConnection seemed to be the best solution to this problem, however, we are experiencing problems with Internet Explorer 9.
private var lc:LocalConnection = new LocalConnection()
try
{
lc.connect ("Main");开发者_开发百科
//continue
}
catch (e:Error)
{
//swf exists.
}
It seems IE9 specifically seems to not destroy the SWF instantly. Refreshing a page can show "Swf Exists" and several seconds later, a refresh fixes the problem.
Does anyone know why this happens?
In failing that. Does anyone know if there is a Exit event? For example, when the SWF is terminated, is there a way i can catch that event and manually close down the local connection before termination?
onApplicationExit(e:Event): void
{
lc.close();
}
Finally, does anyone have any other methods i could use to try to achieve a single swf instance?
you could use a SharedObject to store a flag whether or not one SWF is already open. if the flag is set to true, close the second swf on startup.
and reset the flag when the first SWF closes. maybe easier to handle than LocalConnection.
edit
add my stuff from the comment:
because sharedobjects don't have a lifespan you could set, maybe you could work with some kind of timeout: create a unique ID for your SWF and write the ID and a timecode every 5sec to the SharedObject. whenever the SWF gets opened, you compare IDs and check if the timecode os older than 5-10 seconds. if so, close it, if not overwrite sharedobject. something like that ...
There's no direct way to catch a page close event with flash, but javascript provides you with a way. I'd try the following thing:
Add a window.onbeforeunload javascript listener to your webpage and use it to call a cleanup function in your flash movie (which would close the local connection instantly). To make your flash movie listen to javascript calls have a look at ExternalInterface.addCallback.
In each loaded swf, set a static varible when that clip is initialized. Then in you main clip, check to see if that variable is not equal to null. If that is the case, you have already loaded that swf, and you can ignore the load.
As an added bonus, if you set value of the static variable to the document class of the loaded swf, you will also have a reference to the loaded swf.
精彩评论