“心跳” PHP/JS 从客户端到服务器

发布于 2025-01-04 17:10:45 字数 1107 浏览 3 评论 0原文

我正在尝试创建一个小型网络应用程序,允许用户“登录”和“注销”。 我目前遇到的问题是允许客户端向服务器发送持续的“心跳”或消息以通知它仍然处于活动状态。

这更像是一个逻辑问题。我想做的是在 php 中有一个 while(1) 循环来检查是否跳过了 n 个心跳。我仍然希望客户端和服务器能够在这个循环进行时进行交互(本质上我希望服务器的行为就像它有一个单独的“check_for_heartbeats”线程一样。

如何使用 php 来实现这一点?我正在运行 XAMPP 。

编辑:为了澄清,我想要做的是即使在 window.unload 事件不会触发的情况下也能够捕获浏览器关闭事件(例如客户端与客户端断开连接) 在这种情况下,有一个线程来监视心跳似乎是最直观的解决方案,尽管我不确定如何在 php 中实现它:

编辑 2: isLoggedIn() 只是检查是否存在的辅助函数 。设置会话布尔变量 ($_SESSION['is_logged_in'))。

最终编辑:好的,我现在完全明白评论和回复的意思了。因此,换句话来说,这是潜在的解决方案: 使用 Javascript 代码将“心跳”发送到服务器。服务器将添加与这些节拍相关的时间戳。 修改数据库以保存这些时间戳 查询整个“时间戳”表(更可能是具有“时间戳”属性的“用户”表),并查看现在和最后一个时间戳之间的差异是否大于某个阈值。 “注销”任何超过此阈值的用户。

唯一的问题是,如果只有一个用户登录,或者所有用户同时失去连接 - 但在这些情况下,没有其他人会看到用户失去连接。

这是多种回应的结合,但我认为克里斯的回应解决了大部分问题。感谢 chris 和 Alex Lunix 做出的有益贡献。 :D

这是一个代码片段,可以更好地解释

服务器端:

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 accomplish 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 comments 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 combination 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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃不饱 2025-01-11 17:10:45

我通常做的是使用 javascript (jQuery) 调用 php 文件并更新数据库或任何你喜欢的内容。这个问题可能会回答您的问题: 确定用户是否在线的最简单方法是什么? (PHP/MYSQL)

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)

云胡 2025-01-11 17:10:45

您可以使用 ajax 对更改心跳会话变量的脚本进行心跳检测,并在每个脚本的顶部执行此检查(当然将其放入函数中并调用该函数):

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

编辑:
如果您希望数据库立即反映该状态,而不是在他们下次访问该页面时反映该状态,则需要使用 MySQL。在不使用另一个程序(例如java程序)来检查数据库的情况下,我唯一能想到的就是将其添加到每个页面的顶部(当然在一个被调用的函数中):

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

这将更新每个用户,这意味着登录值的准确性将由页面浏览的频率来设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文