BlazeDS Developer Guide

Session life cycle

Disconnecting from an HTTP-based channel

Because HTTP is a stateless protocol, when a SWF file is disconnected, notification on the server depends on when the HTTP session times out on the server. When you want a Flex application to notify the BlazeDS server that it is closing, you call the disconnectAll() method on the ChannelSet from JavaScript.

In your Flex application, expose a function for JavaScript to call, as the following example shows:

<mx:Application creationComplete="init();">
...
<mx:Script>
    <![CDATA[
        import flash.external.ExternalInterface;
        private function init():void
        {
            if (ExternalInterface.available)
            {
                ExternalInterface.addCallback("disconnectAll", disconnectAll);
            }
        }

    // This function will be called by JavaScript
    private function disconnectAll():void
    {
        // Here you get channelSet of your component and call disconnectAll.
        // For example: producer.channelSet.disconnectAll();
    }
...
</mx:Script>
</mx:Application>

Next, in the body element of the HTML file that wraps your SWF file, you specify an onunload() function, as the following example shows:

<body ... onunload="disconnectAll()" ..>

Finally, you define the JavaScript disconnectAll() function that calls the ActionScript disconnectAll() method, as the following example shows:


<script langugage='javascript' ..>

    function disconnectAll()
    {
        var answer = confirm("Disconnect All?")
         if (answer)
        {
            // Here you'll need to provide the id of your Flex swf
            document.getElementById("yourFlexSwfId").disconnectAll();

            // And it is important that you have some alert or confirm 
            // here to make sure disconnectAll is called before the
            // browser window is closed.
            alert("Disconnected!")
        }
    }
</script>

Invalidating an HTTP session

Another complication with using the HTTP protocol is that multiple SWF files share the same HTTP session. When one SWF file disconnects, BlazeDS cannot invalidate the HTTP session. In this scenario, the default behavior on the server is to leave the current session in place for other applications or pages that are using the HTTP session. However, you can use the optional invalidate-session-on-disconnect configuration property in a channel definition in the services-config.xml file to invalidate the session, as the following example shows:

<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
    <endpoint url="http://servername:port/contextroot/messagebroker/amf"
        class="flex.messaging.endpoints.AMFEndpoint"/>
    <properties>
        <invalidate-session-on-disconnect>true</invalidate-session-on-disconnect>
    </properties>
</channel>


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/blazeds/1/blazeds_devguide/lcconnections_4.html