Home  >  Article  >  Backend Development  >  A brief discussion on PHP+MySQL chat room technology_PHP tutorial

A brief discussion on PHP+MySQL chat room technology_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:331049browse

   近来,在PHPChina推出了PHP聊天室技术的专题,恰好本人最近也在做这个项目,也在此发表一下个人的观点。

   聊天室主要功能:
        1,注册,登陆,退出,资料修改。
        2,用户申请创建聊天室,自定义聊天室属性。
        3,显示聊天室名称,描述,主持人,公告,广告。
        4,实时显示在线用户列表,可以查看在线用户资料(昵称,email,ip,在线时长等)。
        5,发表聊天内容,字体属性,动作,表情,常用短语。
        6,允许隐身,私聊,滚屏,屏蔽用户,清屏,聊天场景选择,背景音乐,发送方式自定义。
        7,管理员有权对成员进行管理(踢出,禁止发言,移交管理权限)。


  注意事项:
        1,禁忌使用框架结构(iframe除了减轻您的劳动量外,并不能带来任何好处)
        2,前台和后台代码彻底分开,只通过ajax通道传送数据,而且只应该传送必要的数据。(前台显示数据,后台处理数据)
        3,禁忌因程序引起的整个页面刷新,只更新需要更改的内容(禁忌使用header,meta刷新页面)。
        4,在提交聊天内容时,不能一味追求用户体验的速度,而忽略了实际的聊天记录提交次序。(即是说,提交的聊天记录应该在服务器重组实际的先后次序,再传送到客户端,因为可能在同一时间有很多人提交信息)


   使用到的技术:
        1,前后台的桥梁——AJAX。可以选用jquery,XAJAX。本人倾向于jquery,速度快,简单耐用,有很多插件,更新的也快。
        2,数据载体的形式——XML。其实一般的聊天室数据也不是很复杂,用json完全够了。
        3,数据库抽象层——ADODB。用PDO更快一些。个人已经习惯使用ADODB。
        4,数据底层存储介质——MySQL。这个没有疑问。
        5,数据中间存储介质——memcache。将高速读写的数据存储到memcache共享内存里面,减轻数据库的负荷。
        6,如果要实现视频和语音,需要服务端FMS(flash media server)的支持,客户端需要安装flash player。并且涉及到在html,javascript,flash与后台相互之间的数据交互。这方面资料不是很多。


    这些是最核心的技术,在实际的应用开发中,还有许多细节问题需要解决。

    比如:怎么处理在线列表的更新?
            途径有这么两种:
                1,刷新整个列表(无论是否有更新)。
                2,添加新上线成员,删除掉线成员,更改资料有变动的成员。
           显然第二种是最佳的选择。

How to deal with XmlHttpRequest concurrency issues?
JavaScript is single-threaded. If there are two XmlHttpRequest objects at the same time, it is easy to cause problems, which requires us to control the generation and end of XmlHttpRequest.
In the chat room, the data we need to update mainly include: online list (including updates of member information), chat records, chat room attributes, etc. The frequency of these updates is inconsistent (if the times are consistent, you only need to create an XmlHttpRequest to handle it). Chat history takes a shorter period of time to update, and online lists can take a little longer to update. Without using a framework, if you create XmlHttpRequest objects separately, concurrency problems are likely to occur. This requires creating a time and process control function.
SetInterVal (process_Control (), 3000) // Dist once every three seconds
Function proces_control to implement the task scheduling, such as: performing the mission for a certain period of time, and the next task is performed when the mission is completed.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486644.htmlTechArticleRecently, a special topic on PHP chat room technology was launched in PHPChina. I happen to be working on this project recently, too. Here is my personal opinion. The main functions of the chat room: 1. Register, log in...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn