Question

I wish for users of a Java chat applet to be automatically logged out when closing the browser window.

I currently use the following:

public void destroy() {
    sendLogoutMessage();
}

However this only works 3/4 of the time (probably due to network delays).

The chat applet pings the server and logs them out after 90 seconds (this allows them to reconnect due to any internet problems) - so they do get removed eventually, however I would like a way to better catch the close event.

Was it helpful?

Solution

I think that your code does not work in 100% cases because it is called in destroy method that does not wait and closes everything including network connections being opened. So, sometimes if network is slow the applet together with its output streams is killed before it sends the logout message to server.

If my theory is correct you can probably check it.

  • Try to see whether you get logout command in server logs (in case of failure). I believe that you will find that command did not arrive.
  • Open java console on client side. You will probably get IOException or SocketException. I hope that examining of the exception may give you an idea how to improve the solution.

Additionally I'd suggest the following.

  1. Add servlet session listener on server side and logout automatically when session is expired.
  2. Decrease time-to-live of HTTP session to reasonable number. Default value for JBoss is 20 minutes, so make it 1 minute.
  3. Implement keep-alive mechanims from your applet. It may be very simple. Just perform HTTP GET every 30 seconds to special URL that does nothing. This will keep your session alive.

This solution should be an addition to your existing solution that works 3/4 of the times.

I hope this helps. Good luck.

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