在PHP + MySQL的+ JavaScript的网络聊天 [英] Web Chat in PHP + MySQL +JavaScript

查看:98
本文介绍了在PHP + MySQL的+ JavaScript的网络聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找创建使用PHP,MySQL和JavaScript的一个网络聊天系统。

I am looking to create a Web Chat system using PHP, MySQL and JavaScript.

目前,我存储在MySQL数据库中的信息与增量ID(是的,这是索引),时间戳,发送者和消息本身。然后,我用我的AJAX来查询数据库中的每个500毫秒,看是否有任何比上一个收到了最新的消息。不过,我有一种感觉,这可能是效率极其低下的,因为它会导致MySQL服务器带来很大的负担,当多个用户是否在线。经四处张望了一下谷歌和就到这里,似乎一切都指向这样做的。

Currently, I am storing messages in a MySQL database with an incremental ID (Yes, it is indexed), a timestamp, the sender, and the message itself. I am then using AJAX to query the database every 500ms, seeing if there are any more recent messages than the last one received. However, I have a feeling that this is probably horribly inefficient as it will result in a large load on the MySQL server when multiple users are online. Having looked around a bit on Google and on here, everything seems to point to this way of doing it.

我的问题:?有没有更好的方式来做到这一点如何减少服务器上的负载任何提示也将受到欢迎。

My question: is there a better way to do this? Any tips on how to reduce the load on the server would also be welcome.

我使用的PHP 5.3,在Apache网络服务器,所以库或插件与兼容的就可以了。

I'm using PHP 5.3, on an Apache webserver, so libraries or plugins compatible with those would be fine.

修改: 忘了提,在原来的职位,但我并不担心支持IE或其他浏览器已经过时

EDIT: Forgot to mention in the original post, but I'm not worried about supporting IE or other outdated browsers.

推荐答案

您应该看看AJAX长轮询,概括地说这是一个简单的Ajax调用,但不会从服务器返回结果,如果没有新的数据。你只是做在服务器端一个简单的循环,直到新的数据将可再返回。当然,你必须停止这最终如果没有结果一段时间后发送给客户端(如1分钟)然后重新启动电话。

You should look into ajax long polling, in a nutshell this a simple ajax call but will not return a result from the server if there is no new data. You just do a simple loop on the server side until new data will be available then return it. Of course you have to stop this eventually if there's no result to send to client after a while (eg. 1 minute) then restart the call.

这篇关于在PHP + MySQL的+ JavaScript的网络聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆