WApplication::finalize - best place?

Added by Mark Travis over 1 year ago

I want to keep a log of how much time my users spend on the app.

I found a work-around for Wt::Auth not telling me which User() just logged out. (Wt::Auth::LoginState::LoggedOut comes through with an empty user pointer.) I just populate a Wt::Dbo::ptr when a user logs on, then when a login signal changes, i check to see if it's populated and set it to null on the Wt::Auth::LoginState::LoggedOut signal.

However, I DO let my users check the "keep me logged in" button that expires every two weeks. For these types of users, I'm not sure how to tell when they are actually using the app and when they aren't. My timer is set such that they can shift focus away from the browser tab for an hour or so and not get logged off.

I don't need to keep track to the minute, but a rough idea of when they are using the app vs not using the app is what I'm looking for.

Is WApplication::finalize() the best place to track a user "timing out"? Is that what gets triggered when an app is about to close due to lack of activity? (Also seems like a good trigger to save the current app/user state.)

I can use Wt::Auth::LoginState::Weak to tell when they come back online after an extended absence. I'm just not sure where the ideal place to record a time-out would be.

Replies (1)

RE: WApplication::finalize - best place? - Added by Roel Standaert about 1 year ago

The WApplication is destroyed as a consequence of:

  • The user navigating away from the page and the unload() event gets through (this is not guaranteed).
  • The session timing out (this is either some time after the user navigated away from the page, or if the connection was dropped for whatever reason)
  • WApplication::quit() being called. This may be as a consequence of:
    • idle timeout, if one is configured in wt_config.xml
    • a JavaScript error, depending on how the <debug> setting is configured
    • your code calling quit()

Just before the WApplication is destroyed, WApplication::finalize() is called. It's just like a destructor, except that it is executed using WApplication::notify() and you can safely call virtual functions in finalize().