开发者

PHP longpolling running endlessly

开发者 https://www.devze.com 2023-04-01 01:00 出处:网络
Iam building a website with chat functionality and I need to keep track of online and offline users. For keeping track of online users I am using this table

Iam building a website with chat functionality and I need to keep track of online and offline users. For keeping track of online users I am using this table

user_id | timestamp

I update the timestamp every 20 seconds for all online users.So I can find out who all are offline by just by comparing the current timestamp with the timestamp in the table.

Now the problem is this: I used long polling to update the online status of the users.That is when the user logs in I run an ajax call to a script that looks like this.

<?php
set_time_limit(0);

while(1){
  updateUser开发者_如何学编程Timestamp();
  sleep(20);
}
?>

The above code is working perfectly.But the problem is that even after the user closes the browser it continues running like a ghost process and keeps on updating the timestamp and taking up resources.I want it to stop when the client closes the browser.

Please help.


Try the following solution (see the documentation on connection_aborted() function):

<?php
set_time_limit(0);

while(!connection_aborted()){
    updateUserTimestamp();
    sleep(20);
}

Alternatively, you can set your script to abort when the client closes the connection. See the documentation on ignore_user_abort for details.

But if you may have many concurrent requests, then maybe it is a good idea to either

  1. resign from using long polling in favor of frequent AJAX requests, or
  2. employ Node.js-like server side solutions that do not use much resources on additional requests.


But the problem is that even after the user closes the browser it continues running like a ghost process and keeps on updating the timestamp and taking up resources.I want it to stop when the client closes the browser.

That's exactly why you don't want to do this. You need to remove this script, and move the updateUserTimestamp() to some global script that is called frequently, ideally the script where the user status is changed (because, if no user status changes, it doesn't have to be updated either).


What server are you running this on? This should not happen, because when the user closes the browser, it should also kill the AJAX request and close the TCP connection, and the server should take note of this and kill the running PHP process/thread.

Try adding a window.onunload client-side event which kills all active AJAX requests and see if that makes a difference.


Your code continues to update the status with no evidence whatsoever that the user is still connected or active. If the user simply shuts off his computer, it will run forever even if it could detect the connection closing. (A computer that is off cannot close a connection, can it?)

You have to make the user do something every 20 seconds that triggers the update. You can decide what that something is, and it can be anything. But there has to be something the user does that makes you consider him active. You can't just consider him active forever, which is what your code does.


To avoid never terminating processes, have the script terminate after its' 20 second wait and force the client to open a new connection. You keep the advantages of the long poll, but will have no more ghost processes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号