"心跳"从PHP / JS中的客户端到服务器 [英] "Heartbeats" from client to server in PHP/JS
问题描述
我正在尝试创建一个允许用户登录和注销的小型Web应用程序。
我目前遇到的问题是允许客户端向服务器发送持续的心跳或消息,以通知它仍处于活动状态。
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.
这是一个更合乎逻辑的问题。我想要做的是在php中有一个while(1)循环来检查是否已跳过n个心跳。我仍然希望客户端和服务器能够在此循环进行时进行交互(基本上我希望服务器的行为就好像它有一个单独的check_for_heartbeats线程。
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.
如何使用php实现这一目标?我现在正在运行XAMPP。非常感谢任何帮助。
How does one accomplish this using php? I am running XAMPP at the moment. Any help would be much appreciated.
编辑:澄清,我想做什么能够捕获浏览器关闭事件,即使在window.unload事件不会触发的情况下(例如客户端从互联网断开连接)。在这种情况下,拥有一个监视心跳的线程似乎是最直观的解决方案虽然我不确定如何在php中实现它。
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.
编辑2:isLoggedIn()只是帮助函数,它检查是否有会话布尔变量( $ _SESSION ['is_logged_in'))已设置。
Edit 2: isLoggedIn() is just helper function that checks to see if a session boolean variable ($_SESSION['is_logged_in')) is set.
编辑最终版好的,所以我现在明白了评论和回复的含义。所以解释一下,这是潜在的解决方案:
有Javascript代码将心跳发送到服务器。服务器将添加与这些节拍相关联的时间戳。
修改数据库以保存这些时间戳
查询整个timestamps表(更可能是带有'timestamp'属性的'users'表),看看NOW和上一个时间戳之间的差异是大于某个门槛。
注销任何超过此阈值的用户。
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.
这是多个回复的组合,但我认为克里斯的回应处理大部分问题。感谢Chris和Alex Lunix的有益贡献。 :D
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
服务器端:
function checkBeats()
{
while(isLoggedIn())
{
// it's been > 30 secs since last heartbeat
if((time() - $_SESSION['heartbeat']) > 30)
{
logout();
break;
}
}
}
推荐答案
我通常做的是使用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)
这篇关于"心跳"从PHP / JS中的客户端到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!