在网站上检查用户新私人消息的最佳方式?
我正在用 PHP 和 Javascript 编写一个网站,该网站实现私人消息传递功能,并且我希望拥有它,以便当用户收到新消息时它会通知他们(通过屏幕顶部闪烁的“邮件”图标)。
问题是如何实现此功能,而不必在指定用户每次加载页面时检查消息数据库中是否有未读消息,这对于每个用户在每次页面加载时执行的服务器负载来说似乎太多了。
有人能建议一种更有效的方法吗?如果用户在收到新消息时没有立即收到通知,也没关系。
谢谢!
I am coding a website in PHP and Javascript which implements private messaging functionality, and i wanted to have it so when a user recieved a new message it notifies them (by a flashing 'mail' icon at the top of the screen).
The question is how to implement this functionality without having to check the messages database for unread messages to the specified user every time they load a page, it seems like too much server load for every user to perform on every pageload.
Can anybody suggest a more efficient method? It doesn't matter if the user isn't notified instantly upon recieving a new message.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了避免每次页面加载时都执行此操作,您可以在计时器函数中使用一些 JavaScript 来调用数据库并检查用户是否收到消息。正如 Am 所说,如果您只是在用户表上设置了一个标志,您只需执行
Now 表示,每次加载页面时运行这样的 select 语句本身确实不会增加服务器上的负载 太多,除非您正在运行一个拥有大量用户的网站。
To avoid doing it every page load you could have some javascript within a timer function that calls to the database and checks to see if the user received messages. As Am said if you just have a flag set on the user table you can just do a
Now that said a select statement like that by itself being ran every time a page loads really won't increase the load on your server that much, unless you are running a site with significant amounts of users.
您可以将一个新字段添加到用户表中,当新消息到达他的收件箱时,该字段将设置为 true。这样,在收到消息时,标志就会被设置,并且您永远不会真正执行查询来检查未读消息。
You can have a new field added to the table of users which gets set to true when ever a new message arrives into his inbox. That way upon receiving messages the flag gets set and you'll never actually do a query to check for unread messages.
您可以将字段“messages”添加到用户表中,并在向用户发送消息时递增该字段。
You can add a field 'messages' to the users table, and increment it when a message is send to an user.
我想说,在每次页面加载时检查消息表中是否有新消息,服务器负载可能不会太多。
通过适当的索引,这根本不应该是一个问题。
0.02 美元
I would say that it's likely not too much server load to check the messages table for a new message at every page load.
With proper indexing, this shouldn't be a concern at all.
$0.02