Question

I need to make sure that only one instance of a SWF is open at any one time on a computer, whether it is in 2 browser windows, or two different browsers.

How can I go about doing it?

I have so far thought of two potential solutions:

1) Using a Flash sharedobject - but since they never expire, if the user closes the browser window without calling my quit function to remove it, the remaining object will interfere with other new sessions. (Perhaps a random number and timestamp to identify the instance?)

2) Using a localconnection - a bit hackish, this relys on the swf detecting whether a Localconnection object is being used, and not loading if that is the case. But localconnections seems to crash at the slightest provocation, so I am a bit wary of using it.

Was it helpful?

Solution

I believe LocalConnection has one advatage over SharedObject solution - it allows global-scope LocalConnection names, though local SharedObjects are always stored per-domain, as far as I know.

As for SharedObject, the only option is to keep regularly updated timestamp in it, and check it every time your .swf runs.

So I'd opt for a simple LocalConnection-based solution, because it's quite straightforward, and I've never experienced any problems with LocalConnection.

public class LCTest extends Sprite
{
    public function LCTest()
    {
        var lc:LocalConnection = new LocalConnection();
        lc.allowDomain("*");
        try
        {
            lc.connect("_myLCLock"); // underscore for global scope
            trace('not running, can init');
        }
        catch (e:ArgumentError)
        {
            trace('already running');
        }
    }
}

OTHER TIPS

If the SWF is only available from one location, I think Shared Objects are a safe bet. I don't think there's any need to worry about complicated sessioning, there should be a shutdown event you can catch even when the browser is manually closed. (Even if there isn't, I'm 99% sure that SOs are flushed on shutdown no matter what, so some workaround should be possible.)

However, Shared Objects are intrinsically linked to the location of the SWF, so a user could sidestep the above method by viewing a second copy of the SWF that they had saved locally, or uploaded to a different web server, or whatever. If that's a worry, and if the SWF would still work in such a situation, then I think LocalConnection is your only choice. But I've never found LC to be very reliable either. I don't know if it could be made bulletproof, but I think it's the only other option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top