tags:

views:

422

answers:

2

I'm looking to dispose of my localconnection when the movie is closed, or unloaded, what event should i do this with?

A: 

I guess you could react to the unload event in JavaScript and call some cleanup function in the flex app through the ExternalInterface. Haven't worked much with ExternalInterface though, so I'm not really sure.

Jörg Reichardt
+1  A: 

I do not believe that there is, but I think you can force the closure anyway.

Not entirely certain how simple this will be, but here would be my best guess. When instantiating your LocalConnection, be sure to include this in the client (or as a property in AS2):

function close()
{
    myConnection.close();
}

In addition to that, I would include this while attempting to have the connection connect:

var commName:String = "MY_CONNECTION";
var myConnection:LocalConnection = new LocalConnection();

// Try to tell any open LocalConnection on this channel to close.
// This may cause an AsyncErrorEvent, so be sure to add the appropriate 
// Error handling.
myConnection.send( commName, "close" );

try
{
    myConnection.connect( commName );
}
catch( error:Error )
{

    // If there is another connection already open on the same channel,
    // that will cause an Error.  I have had some luck catching that 
    // Error and then calling connect again.  That said, you would be 
    // best to take precautions anyway.
    try
    {
        myConnection.connect( commName );
    }
    catch( error:Error )
    {
        // Your connection cannot connect!!!
        // DO SOMETHING!!!
    }
}
myConnection.client = this;
Christopher W. Allen-Poole
this looks good. i'm interested in closing the connection explicitly because i understand that connection names have to be unique, and if the page is reloaded i dont want the connection to be floating around and causing problems.
Irwin