How can I tell Python / Tornado that the client has closed the tab / browser?

I have been looking for quite some time to solve this, but without the dice.

Edit: I did not indicate that I was trying to create a chat server. Therefore, users are registered, their identifier is added to the list of users and listeners . And they start to chat. But when one of them tries to close the tab or browser, the user will never be removed from both lists, so he / she will remain on.

Edit2: I thought the numbering above was a bit confusing, so I placed the part in the script as well as at the bottom.

So far I have tried the on_connection_close () function (which never works, I don’t know why), the on_finish() function (which runs every time finish() is called), so this also does not match the count.

Now I came up with a bit of a solution that includes the on_finish() function:

  • Whenever the function UpdateHandler class' post() is self.done = 0 , self.done = 0 .
  • As soon as the finish() function starts, I set self.done = 1 .
  • Now the on_finish() function is called and I type self.done on the console and it is 1.
  • In the same on_finish() function, I make the IF self.done = 1 , as expected, it returns TRUE and Tornado io_loop.add_timeout with parameters time.time()+3 (so that it time.time()+3 for 3 seconds to make sure if the user has gone to another page on the website or left the website completely) and a callback that will ultimately be called.
  • After 3 seconds, I want to check if self.done remains 1 or if the user is still on the website, then 0 will suffice.

btw, every 30 seconds the server terminates the connection, and then sends a notification to the user to initiate a new connection so that the connection never depends on it. When the client closes the browser and a 30-second timeout expires, the server tries to send a notification if the client is still on my website, then it initiates a new connection, thereby calling the post() function in the above UpdateHandler class above, thereby changing self.done to 0. (That's why I gave io_loop.add_timeout margin of 3 seconds.)

Now that this has taken care, I wanted to go ahead and try to see how it works. I started the server and opened a browser oriented to the correct URL and watched how the server responded (by putting several print statements in the script). When the user remains connected, I see that after calling post () (which is displayed at this time self.done = 0 ), he sleeps for 3 seconds and then the callback function is called, but this one prints self.done = 1 , which is strange.

I know this is not the most efficient way, but this is the only solution I could come up with that didn't even work properly.

Conclusion:

I hope that someone has a good alternative, or maybe even a point in my theory, which I missed, which violates all this. I really would like to know how to tell Tornado that the client closed the browser before the 30-second timeout. Maybe with pinging an open connection or something else. I looked at TORNADIO , but I didn’t like it. I want to do this in a clean Tornado, if possible, of course.

I will send the code ASAP, I tried, like half an hour, looking at "How to format", etc., but when I try to send my edit, it gives an error.

  • Your message seems to contain code that was not correctly formatted as code. Please delay all code by 4 spaces using the code toolbar or the CTRL + K key combination. For more editing help, click the toolbar icon [?].
+4
source share
2 answers

I had this problem for 5-6 days and finally figured out what the problem was, well .. not really, but it was solved! I searched the Internet but found nothing. In the above message, I said that I remember how it worked when a couple of months ago I tried the same script, but I never mentioned the use of nginx . I struggled with Apache + mod_proxy, but I don't know what the problem is with apache , but when I tried nginx , this time it just works!

If you have the same problem (on_connection_close does not start) "TRY" nginx . Thanks for your help too @Nikolay.

0
source

There is an on_close() method tornado.websocket.WebSocketHandler to handle the tab / browser close event. Here is an example:

 from tornado.websocket import WebSocketHandler class MyWebsocketHandler(WebSocketHandler): ... def on_close(self): self.application.database.abort() 

and link .

0
source

Source: https://habr.com/ru/post/1434711/


All Articles