Flex Pasta » User Closes the Browser or Navigates Away in Flex

User Closes the Browser or Navigates Away in Flex

Whether I am tracking how long a user’s session lasts, trying to decrease JVM memory usage, or am debugging a user’s problem, I want to know, and end immediately the user’s Java session when they(the user) logs out of my flex application. When they click my big red “Log Out” button like every good user is supposed to then I can end the user’s session easily by calling a remote service that does

try
{
FlexContext.getFlexSession().getSession(false).invalidate();
}
catch(IllegalStateException i)
{
System.Out.Println("I'm trying to invalidate the session but it seems to be dead already. Oh and I'm also using System Out which is not such a great idea, but hey, this is just an example!!!");
}

Now evil Sam comes along and doesn’t press the nifty log out button I so thoughtfully left for him. Instead he navigates away or closes his browser. How to catch this in flex? First, I must have an html wrapper around my swf file. If I don’t, this solution has no basis. In the html wrapper file, find the body tag and add an “onunload” attribute.

<body onunload=”MyFlexApp.myFlexFunction();”>

Then in myFlexFunction(the one inside your swf), I can call the normal Logout service just as if they pressed the logout button.

1 Comment

  • 1. But.. replies at 9th June 2008, 12:56 pm :

    But if the browser crashes, or the machine loses power, or the user’s connection dies, you still won’t get any notification on the server even with this.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>