Check if user closed the page in PHP?

前端 未结 9 1939
孤城傲影
孤城傲影 2020-12-22 06:04

I made a chat using PHP and JavaScript chat and there is a disconnect button which removes user from the chat removing him from user list first. But if the user closes brows

相关标签:
9条回答
  • 2020-12-22 06:52

    Sorry, there is no reliable way of doing this, that's the way HTTP was built - it's a "pull" protocol. The only solution I can think of is that "valid" and logged in clients must query the server in a very small interval. If they don't, they're logged out.

    0 讨论(0)
  • 2020-12-22 06:55

    you could send a tiny ajax call to your server every 5 seconds. and users that doesn't do this aren't in the room any more

    0 讨论(0)
  • 2020-12-22 07:06

    You answered your own question: if you don't detect a request for new messages from a user over a given length of time (more than a few seconds), then they left the room.

    The nature of HTTP dictates that you need to do some AJAX type of communication. If you don't want to listen for the "give me more messages" request (not sure why you wouldn't want to), then build in a heartbeat type communication.

    0 讨论(0)
提交回复
热议问题