如何知道用户何时关闭浏览器?聊天应用

发布于 2024-10-07 15:19:00 字数 328 浏览 1 评论 0原文

我设置了一个简单的聊天客户端,允许用户使用用户名登录并将他们编写的消息存储在 SQL 数据库中。每 3 秒,数据库就会简单地打印所有行。所以它基本上是一个聊天客户端。

我想保留一份在线人员名单。我该怎么做?我如何感知有人关闭了浏览器?

现在我正在提取用户名

$name = $_COOKIE["name"];

,如果这个值是空的,我知道他们离开了。但一旦他们离开,就为时已晚,无法知道他们的用户名是什么,所以我无法跟踪到底是谁离开了。

有想法吗?我对 php、javascript 和 html 还很陌生,所以请记住这一点:)

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 :)

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

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

发布评论

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

评论(6

笨死的猪 2024-10-14 15:19:00

当有人关闭窗口时,很难向服务器发送最后一个请求,因为当用户希望关闭窗口时,浏览器通常不会等待 JS 执行完成(onbeforeunload 就是这种情况) )。

每当我遇到这样的情况时,我倾向于使用 onbeforeunload 发送最终请求(该请求发生得很快,通常在浏览器窗口关闭之前完成),但也会实现超时功能。超时功能的工作原理如下:

每次用户向服务器发送某些内容时,服务器都会将其识别为“仍然存在”。同时,客户端设置一个计时器,例如 45 秒。如果用户在 45 秒内没有键入任何内容,客户端会自行发送“仍然有效”信号以保持连接。

现在,服务器应该每 60 秒执行一次 removeInactive() 例程(允许 15 秒的慢速连接余量,因此为 45/60 秒),这将删除任何未发送“仍然活着”的用户' 在最后 60 秒内发出信号。

到目前为止,这个系统对我来说效果很好,你可以自己尝试一下。

It is hard to send a last request to the server when someone closes the window, since Browsers usually don't wait for JS execution to finish when the user wants the window closed (as is the case with onbeforeunload).

Whenever I am confronted with a situation like this, I tend to use onbeforeunload to send a final request (which happens fast and usually finishes before browser window is closed), but also implement a timeout feature. The timeout feature would work as follows:

Everytime the user sends something to the server, the server recognizes this as 'still there'. At the same time, the client sets a timer of, say, 45 seconds. If the user does not type anything for 45 seconds, the client sends a 'still alive' signal on its own to stay connected.

Now, the server should execute a removeInactive() routine every 60 seconds (allow 15 seconds slow-connection margin, hence the 45/60 seconds) which removes any users who haven't sent a 'still alive' signal in the last 60 seconds.

This system worked fine for me so far, you could try it out for yourself.

£噩梦荏苒 2024-10-14 15:19:00

假设您使用 AJAX 每 X 秒提取一次聊天消息,请使用该用户的当前时间戳更新当时的用户表。然后任何有时间戳的人说超过 10 秒,你就会知道他们已经离开了页面。

Assuming your using AJAX to pull in the chat messages every X seconds, update your table of users at the time with the current timestamp for that user. Then anyone who has a timestamp say older than 10 seconds, you will know they have left the page.

放飞的风筝 2024-10-14 15:19:00

将在线用户放入一个表中,该表将有一个名为“lastSeen”的字段,每隔几秒使用 ajax 调用更新此字段。ajax

调用如下所示:

window.setInterval(function() {
    $.ajax({      
      url: _URL_ENGINE + "/updateLastSeen/?userid=" + userID,
      success: function(data) {

      }
    }); 
}, 2000); // 2000 means 2 seconds

现在要查询在线玩家列表,您可以像

select * from players WHERE lastSeen > DATE_SUB(NOW(), interval 40 SECOND) 

希望这样 查询它们帮助

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

鸢与 2024-10-14 15:19:00

使用 onbeforeunload 事件。

Use the onbeforeunload event.

嘴硬脾气大 2024-10-14 15:19:00

您可以附加到 JavaScript 中的 onunload 事件。请参阅 http://help.dottoro.com/ljflhicd.php 了解完整详细信息。

You could possibly attach to the onunload event in javascript. Have a look at http://help.dottoro.com/ljflhicd.php for full details.

闻呓 2024-10-14 15:19:00

请参阅这些链接。当然,这是照亮你黑暗的光明。

链接1:
http://www.daniweb.com/forums/thread252828.html

Link2 :
http://phpeasystep.com/phptu/9.html

Refer these links. Sure this is a light for ur darkness.

Link1:
http://www.daniweb.com/forums/thread252828.html

Link2:
http://phpeasystep.com/phptu/9.html

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