如何知道用户何时关闭浏览器?聊天应用 [英] How to know when user closes browser? Chat application
问题描述
我有一个简单的聊天客户端设置,允许用户使用用户名登录并将他们写入的消息存储在sql数据库中。每3秒钟,数据库就会打印所有行。所以它基本上是一个聊天客户端。
我想保留谁在线的列表。我怎样才能做到这一点?当人们关闭浏览器时,我该如何感觉?
现在我将用户名称作为
$ name = $ _COOKIE [name];
如果这个值是空的,我知道他们离开了。但是一旦他们离开了,知道他们的用户名是什么时候已经太迟了,所以我无法追踪到底是谁离开了。
想法?我相当新的PHP,JavaScript和HTML,所以请记住:)
解决方案表将会有一个名为'lastSeen'的字段,每隔几秒用ajax调用来更新这个字段。
ajax调用可以这样做:
window.setInterval(function(){
$ .ajax({$ b $ url:_URL_ENGINE +/ updateLastSeen /?userid =+ userID,
success:function(data){
}
});
},2000); // 2000的意思是2秒
现在查询在线玩家列表, p>
select * from players WHERE lastSeen> DATE_SUB(NOW(),间隔40 SECOND)
希望这可以帮助您
I have a simple chat client set up that allows users to login with a username and stores the messages they write in an sql database. Every 3 seconds, the database simply prints of all the rows. So it's basically a chat client.
I'd like to keep a list of who's online. How can I do this? How can I sense when someone has closed the browser?
Right now I'm pulling the username as
$name = $_COOKIE["name"];
and if this value is empty, I know they left. But once they left, it's too late to know what their username was so I can't keep track of who exactly left.
Ideas? I'm fairly new to php, javascript, and html, so keep that in mind :)
put online users in a table that will have a field named like 'lastSeen' update this field every few seconds with ajax call..
ajax call be made someting like this :
window.setInterval(function() {
$.ajax({
url: _URL_ENGINE + "/updateLastSeen/?userid=" + userID,
success: function(data) {
}
});
}, 2000); // 2000 means 2 seconds
now to query the list of online players u can query them like
select * from players WHERE lastSeen > DATE_SUB(NOW(), interval 40 SECOND)
hope this helps
这篇关于如何知道用户何时关闭浏览器?聊天应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!