I'm trying to write a simple web chat app with PHP and AJAX.

我试着用PHP和AJAX编写一个简单的web聊天应用程序。

I need to be aware of all the open sessions so that I can display a list of online users that are available to talk to. I also need to be aware of log outs because I use "both the sending and the receiving party are offline" as a condition for considering a chat session terminated and deleting the messages.

我需要知道所有的开放会话,这样我就可以显示可以与之交谈的在线用户列表。我还需要注意注销,因为我将“发送方和接收方都是脱机的”作为考虑终止和删除消息的聊天会话的条件。

I'm keeping track of logged in users in a database: adding an entry on log-in and removing it on log-out works fine, but it's not comprehensive, as there are two other ways a user can become logged out:

我一直在跟踪数据库中的登录用户:在登录时添加一个条目,在注销时删除它,效果很好,但并不全面,因为用户可以通过另外两种方式注销:

  • server side session expires after inactivity.
  • 服务器端会话在不活动后过期。
  • client side cookie gets destroyed on browser close. Seems like a bad idea to use some sort of onclose triggered AJAX (what if the browser crashes or something?).
  • 客户端cookie在浏览器关闭时被销毁。使用某种onclose触发的AJAX似乎是个坏主意(如果浏览器崩溃了怎么办?)

Simplest solution seems to be keeping a timestamp of last activity. I see some problems with this though:

最简单的解决方案似乎是保存上一次活动的时间戳。但我看到了一些问题:

  • AFAIK server-side expiry is chance based, so it wouldn't be accurate (and if I get the expiry time 3 minutes wrong, that's 3 minutes where some guy could be talking to an offline user wondering why no one is answering)
  • AFAIK服务器端过期是基于机会的,所以它不会是准确的(如果我的失效时间是3分钟,那是3分钟,有些人可能会和一个离线用户聊天,想知道为什么没有人回答)
  • I'd have to constantly be querying the database to check every logged in users' last activity time compared to the current time. I don't see when / where I'd do this efficiently. It seems stupid to do this every single time the list of online users is needed.
  • 与当前时间相比,我必须不断地查询数据库,以检查用户上一次的活动时间。我不知道什么时候/在哪里我能有效地做这件事。每次需要在线用户列表时都这么做似乎很愚蠢。

Any help appreciated. I'm coding this myself because I'm not aware of any web chat frameworks that can integrate with my existing user database, do correct me if I'm wrong.

任何帮助表示赞赏。我自己编写这个代码是因为我不知道有任何web聊天框架可以与我现有的用户数据库集成,如果我错了请纠正我。

7 个解决方案

#1


1

I don't think you're going to be able to do much to mitigate the constant querying to determine if users have logged off by closing the browser, internet connection issues, etc., but you could perhaps have each client make an AJAX request to the server every 5 seconds to update the last activity time, and have your application on the server consider the user "logged off" if they have missed 3-4 consecutive requests (ie, their last activity time is > 20 seconds).

我不认为你能做得减轻常数查询来确定用户注销关闭浏览器,网络连接问题,等等,但是你可能有每个客户端向服务器发出一个AJAX请求每5秒钟更新最后的活动时间,和您的应用程序服务器上的考虑用户“注销”,如果他们错过了3 - 4个请求(即最后一次活动时间> 20秒)。

On the client side, you could then check the last activity time every time your client sends a message to another user, and respond that they'd logged off if that had happened. If they attemped to open a chat with another user, you could also do an immediate call to check their status. Then you could perhaps check the status of all users in the user list every 30 seconds. That way your client gets pretty quick feedback if the person (s)he is chatting with drops offline unexpectedly.

在客户端,您可以在每次客户端向另一个用户发送消息时检查最后一次活动时间,并响应如果发生这种情况,他们已经注销了。如果他们试图打开与另一个用户的聊天,你也可以立即打电话检查他们的状态。然后,您可以每隔30秒检查用户列表中所有用户的状态。这样你的客户就会得到非常快的反馈,如果他正在聊天的人意外地离线。

更多相关文章

  1. php-fpm通过request_slowlog_timeout检查哪个脚本执行时间长
  2. WordPress注册和用户注册是分开的
  3. PHP联系表格给用户发送随机确认号码
  4. 仅在LARAVEL或AJAX中提交表单时,无需用户身份验证即可将文件上载
  5. php时间函数——获取过去24小时内每个小时的节点
  6. 将纪元时间转换为日期PHP
  7. 如何区分socket.io聊天中的Symfony用户角色/组
  8. 如何减去两个日期和时间来得到不同?
  9. php微信网页授权获取用户信息

随机推荐

  1. Android Layout Tricks #2: Reusing layo
  2. Android 程序启动界面Demo
  3. Android实现音乐的播放与停止(Service的初
  4. Android中Intent对应的category列表大全
  5. 垂直跑马灯
  6. 【Android】Android WebView 网页输入框
  7. react-native02:安卓返回键控制
  8. Android 调用系统相册选择图片并显示
  9. Android实现振动效果
  10. Android studio 打包问题:Lint found fata