最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Heartbeats" from client to server in PHPJS - Stack Overflow

programmeradmin2浏览0评论

I am trying create a small web application that allows a user to "login" and "logout." What I am currently having a problem with is allowing the client to send constant "heartbeats" or messages to the server to notify that it is still active.

This is more of a logical question. What I want to do is have a while(1) loop in php that checks if n number of heartbeats have been skipped. I still want the client and server to be able to interact while this loop is going on (essentially I want the server to behave as if it has a separate "check_for_heartbeats" thread.

How does one acplish this using php? I am running XAMPP at the moment. Any help would be much appreciated.

Edit: To clarify, what I want to do is be able to catch a browser close event even on instances where the window.unload event won't fire (e.g. a client gets disconnected from the internet). In this case, having a thread to monitor heartbeats seems to be the most intuitive solution, though I'm not sure how to make it happen in php.

Edit 2: isLoggedIn() is just helper function that checks to see if a session boolean variable ($_SESSION['is_logged_in')) is set.

Edit Final: Okay, so I now understand exactly what the ments and responses were saying. So to paraphrase, here is the potential solution: Have Javascript code to send "heartbeats" to the server. The server will add a timestamp associated with these beats. Modify the database to hold these time stamps Query the entire "timestamps" table (more likely a 'users' table with a 'timestamp' attribute), and see if the difference between NOW and last timestamp is greater than some threshold. "Log off" any users passed this threshold.

The only issue is if there is just one user logged in or if all users lose connection at the same time - but in these cases, no one else will be there to see that a user has lost connection.

This is a bination of multiple responses, but I think chris's response takes care of the majority of the issue. Thank you to both chris and Alex Lunix for the helpful contributions. :D

Here is a code snippet for a better explanation

Server Side:

function checkBeats()
{
    while(isLoggedIn())
    {
        // it's been > 30 secs since last heartbeat
        if((time() - $_SESSION['heartbeat']) > 30)
        {
            logout();
            break;
        }
    }
}

I am trying create a small web application that allows a user to "login" and "logout." What I am currently having a problem with is allowing the client to send constant "heartbeats" or messages to the server to notify that it is still active.

This is more of a logical question. What I want to do is have a while(1) loop in php that checks if n number of heartbeats have been skipped. I still want the client and server to be able to interact while this loop is going on (essentially I want the server to behave as if it has a separate "check_for_heartbeats" thread.

How does one acplish this using php? I am running XAMPP at the moment. Any help would be much appreciated.

Edit: To clarify, what I want to do is be able to catch a browser close event even on instances where the window.unload event won't fire (e.g. a client gets disconnected from the internet). In this case, having a thread to monitor heartbeats seems to be the most intuitive solution, though I'm not sure how to make it happen in php.

Edit 2: isLoggedIn() is just helper function that checks to see if a session boolean variable ($_SESSION['is_logged_in')) is set.

Edit Final: Okay, so I now understand exactly what the ments and responses were saying. So to paraphrase, here is the potential solution: Have Javascript code to send "heartbeats" to the server. The server will add a timestamp associated with these beats. Modify the database to hold these time stamps Query the entire "timestamps" table (more likely a 'users' table with a 'timestamp' attribute), and see if the difference between NOW and last timestamp is greater than some threshold. "Log off" any users passed this threshold.

The only issue is if there is just one user logged in or if all users lose connection at the same time - but in these cases, no one else will be there to see that a user has lost connection.

This is a bination of multiple responses, but I think chris's response takes care of the majority of the issue. Thank you to both chris and Alex Lunix for the helpful contributions. :D

Here is a code snippet for a better explanation

Server Side:

function checkBeats()
{
    while(isLoggedIn())
    {
        // it's been > 30 secs since last heartbeat
        if((time() - $_SESSION['heartbeat']) > 30)
        {
            logout();
            break;
        }
    }
}
Share Improve this question edited Feb 12, 2012 at 18:48 funseiki asked Feb 12, 2012 at 2:43 funseikifunseiki 9,53710 gold badges40 silver badges62 bronze badges 5
  • Not sure. I figure yes, because I want to be able to catch issues like if the user's browser has crashed (in that case the window.onbeforeunload event isn't fired and I have nothing other than the heartbeats to make an ajax request). – funseiki Commented Feb 12, 2012 at 7:38
  • 2 I'm still confused what you need this for. What is the goal, to make sure a user is still logged in? Why can't you just check every time they make a request, and log the last request time as a measure of activity? – No Results Found Commented Feb 12, 2012 at 7:52
  • Sorry about the confusion. Yes, I want to make sure the user is still logged in. The heartbeats on the client side (i.e. ajax requests from javascript code) allow me to make sure a user is still online, but where do I do the actual "checking" to make sure they are not offline? Right now, I have it so that anytime a window.unload event is fired the client sends an ajax message saying it the window has closed. Anytime the onload event is fired, the client tells the server that the window is open. Now, what about if the client's internet connection fails - how does the server find out? – funseiki Commented Feb 12, 2012 at 7:56
  • php doesn't do things without people visiting pages, so in order to check the heartbeat time and set them as logged off you'll either need another programming language checking, or you need to incorporate a system in which it checks all of the heartbeat times at each page load and hope that people visit enough to refresh the database. – John V. Commented Feb 12, 2012 at 15:25
  • I was just hoping for consistency in the database. If a user loses internet connection, there is really no way for the server to find out because no heartbeats will be sent. If I implement it in your suggested way (which is pretty much how I'm doing it now), the user will appear "logged in" for the entire time that his/her internet has been disconnected until the user decides to visit the page again. This is problematic primarily for other users that may be looking for people who are also "logged in" and may be talking to someone who appears logged in but has actually lost connection. – funseiki Commented Feb 12, 2012 at 18:17
Add a ment  | 

2 Answers 2

Reset to default 2

What i usually do is call a php file using javascript (jQuery) and update a database or whatevery you like. This question might answer yours: Whats the easiest way to determine if a user is online? (PHP/MYSQL)

You could use ajax to heartbeat a script that changes the heartbeats session variable, and just at the top of every script do this check (put it in a function and call that of course):

// it's been > 30 secs since last heartbeat
if((time() - $_SESSION['heartbeat']) > 30)
{
    logout();
}

Edit: If you want the database to reflect that status instantly instead of when they next visit the page, you'll need to use MySQL. Without using another program (such as a java program) to check the database the only thing I can think of is to add this at the top of every page (in a function that gets called of course):

mysql_query("UPDATE `users` SET `loggedin`=0 WHERE heartbeat<".time()-30);

Which would update every user, which means the accuracy of the loggedin value would be set by the frequency of page views.

发布评论

评论列表(0)

  1. 暂无评论