Execute server method when closing the browser

Asked

Viewed 642 times

3

I need the user to close the browser or the page tab, update the status Logged in that is in the database (to control user access).

For this use the following

Method JavaScript:

window.onbeforeunload = function (event) {
    PageMethods.LogOut();
}

And the next WebMethod:

[System.Web.Services.WebMethod]
public static void LogOut()
{
     //Atualiza status do usuário
}

The problem is that this method is called at all times, when I change the page, when I update the page(F5), how do I fix this?

  • Perhaps it’s best to switch the event to window.onclose = Function(){}, more context in this link

  • I tried with onclose, but it doesn’t work.

  • @Gabrielsantosreis, what you can do is add a Library to the a and of input:submit, as well as in the keypress to monitor F5 (keycode: 116), if something like this occurs you set a flag stating that LogOut should not be called... Unfortunately this approach will only minimize your problem as a manual refresh (browser button) will trigger the Logout.

  • @Tobymosque, really the F5 is the least of my problems, it helps me very little.

  • I saw this example, tested in jsfiddle and apparently works, except when you close the whole browser....

  • It is not possible to detect the closing of windows with javascript, this event detects downloading that is different from closing, that is every closure makes the unonload, but not every unonload comes from closures, here I explained well how this occurs and when we can use it http://answall.com/a/113155/3635 --- I also mentioned about the use of window.popstate, using it you could be able to differentiate pagination from some closures

Show 1 more comment

2 answers

1

HTTP 1.4 protocol is by definition does not wait state on connection from the client to the server, in short there is no secure way to ensure that a user has actually disconnected from their server. But there are techniques that can help you in this task.

As our friend Renan quoted, perhaps the best known and easiest would be the long Polling where the client consults the server from time to time and you specify a time between limit requests to consider it disconnected.

It is also possible to mix this technique with the use of websockets or SSE.

This problem will be solved with the HTTP2 protocol.

1

I know a way to do what you want, but consumes makes the application heavier.

The concept is basically the following:

The logged-in user has a javascript script running, setInterval, answering "call" to the server from time to time.

Every 4 seconds, for example, a record is inserted in the user table with the user ID and a current datetime / timestamp. *Before the query, you can delete the previously entered records, after all only what matters is the latest user Insert.

The method that checks whether the user is online or not searches in the user table_activity the last time the user answered "present" and checks in the same query with a DATE_DIFF if the last time is more than 4 seconds compared to the current date, for example.

NOTE: The time may vary according to your need.

If necessary I can post the code.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.