How do I do something ON EXIT

0 favourites
  • 2 posts
From the Asset Store
Prevent the ball from exiting the screen for as long as you can!
  • How do you run code immediately before the user presses the [X] on the browser ?

    Im trying to update a variable on the server using a AJAX request when the user exits out in order to log them out.

    I tried "on end of layout" but that only works when the user changes layouts, not exits the entire browser... or shuts off their computer etc

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Web browsers don't have a way to process calls on exit. However, to use a proper log in system, all you need to do, is create a session variable on start of the program and set it on the server side. Each time the client tries to communicate to the server, it compares the local variable with that on the server and, if they are different, then the user is logged out. A simple way to implement this is just to use the time stamp of when the game first connected.

    The events would go something like this:

    On start of game: set local session variable to empty

    If local session variable is empty: User signs in and the session variable is set to current time stamp

    If local session variable != server session variable: User signs in and the session variable is set to current time stamp

    If client fails to get a response from the server: set session variable to empty

    If client can contact server and local session variable = server session variable: client is logged in

    This is only one way to handle disconnecting a client. Of course, I would supplement this with a time out so if the client had not transmitted to the server after say 5 minutes, the client would have to sign back in.

    If you haven't yet, you should read up on web page security for sign in pages using session variables.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)