如何检测传入的聊天消息?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用轮询,但您可以使用一种称为 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))
我认为轮询是 JavaScript 推送服务器端更改的唯一方法。
I think polling is the only way for JavaScript to be pushed server side changes.