如何检测传入的聊天消息?

发布于 2024-09-08 06:46:30 字数 318 浏览 2 评论 0原文

我使用 PHP、AJAX、MySQL 数据库和大量 jQuery 来构建基于 Web 的聊天系统的原型(类似于 Facebook 聊天)。我被困在如何“监听”传入的聊天...何时知道有人试图与我聊天...并知道这是一个新的聊天,而不是现有的聊天。

现在,我正在轮询数据库表中是否有新的插入,但它似乎效率很低......服务器的大量开销。

例如,当在 MySQL 数据库的表中插入一行时,有没有一种方法可以接收通知,这样我就可以收到通知,然后查看插入的内容,而不是不断轮询?

如果有更好、更有效的方式来创建这种一对一的聊天关系,请给我一些建议。

谢谢,赫里斯托

I'm using PHP, AJAX, a MySQL database, and a lot of jQuery to prototype web-based chat system (similar to Facebook Chat). I'm stuck on how to "listen" for incoming chats... when to know someone is trying to chat me... and to know that it is a new chat, not an existing chat.

Right now, I'm polling to see if there has been new insertions in the database tables but it seems very inefficient... a lot of overhead for the server.

Is there a way to receive a notification when, for example, a row has been inserted in a table in a MySQL database so that instead of having constantly poll, I can just be notified and then go look at what as inserted?

If there is a better and more efficient way to create this one-on-one chat relationship, please give me some suggestions.

Thanks, Hristo

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

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

发布评论

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

评论(2

情何以堪。 2024-09-15 06:46:30

您必须使用轮询,但您可以使用一种称为 Comet 的技术,该技术涉及长轮询,即发送一个 ajax 请求,该请求将由服务器保留,直到收到聊天请求为止。

http://en.wikipedia.org/wiki/Comet_(编程))

You have to use polling, but you can use a technique called Comet which involves long-polling, i.e. sending out an ajax request that will be held by the server until a chat request comes in.

http://en.wikipedia.org/wiki/Comet_(programming))

时光清浅 2024-09-15 06:46:30

我认为轮询是 JavaScript 推送服务器端更改的唯一方法。

I think polling is the only way for JavaScript to be pushed server side changes.

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