PHP Chat Room Technology

Source: Internet
Author: User
Keywords PHP Chat Room Technology
Tags telnet program

PHP Chat Room Technology
Wong Kwok Fai

1. Objective
Chatting online is the most popular way to make friends nowadays. The chat rooms are featured on each major website.
Chat rooms are mainly divided into webchat, bbschat two kinds. Bbschat is a Telnet-based TCP protocol that is attached to the BBS and requires a client Telnet program. Webchat is browser-based and is actually a CGI program used by many people. The basic principle is to send each user's speech through the browser to the system, and then the system is collected and processed and distributed to specific users.
Webchat generally uses server push or client pull technology. The difference between the two technologies is that the data is distributed to users in different ways, and server push is used by the server to encode the data in multiple MIME-coded, push-to-consumer-side, and now fewer sites use this way. The client pull is the data that the user pulls from the server.
The most commonly used client pull is to use the HTML META tag to http-equiv= the "Refresh" attribute, checking the server for new data every once in a while. For example, the page will be refreshed every 5 seconds. This method is simple and effective, the drawback is that flashing occurs when the refresh, and in order to maintain efficiency, each refresh will be the old chat content, users want to view or retain the conversation is inconvenient. To this end, using Javaapplet as the front end of the chat room, using the refresh to pull the data from the server through the Javaapplet to display is also a solution. There is a scenario in this article that keeps the chat program connected. Method one, the chat program is configured to infinity, so that the browser can keep the download of the connection status; Method Two, there is an infinite loop in the chat program, because it is easy to configure more advanced features, so choose to use this.
The Web server uses Freebsd+apache because the combination of the two has the strongest performance and it costs zero. What else to consider is how to store the data. Document is relatively easy to implement, but many people use, frequently to the same document IO operation, inevitably affect the efficiency, and FreeBSD IO performance is poor. The ability to consider using RAMDisk to put the entire document into memory to increase speed. or partition a high-speed space in memory for data storage. I am using the database method: MySQL. Because the database is designed for the simultaneous use of high-volume users, the speed can be ensured by using the complex authoring that allows the design of high-speed space operations to be avoided. If you put the entire database into memory, the effect is better.
The program is written in Php+html+javascript. The chat room is primarily about the various elements in the HTML form. JavaScript is an object-based language that treats all elements of HTML as objects, so the methods and properties of each element are rich and easy to operate. PHP can only process user-entered data by turning the elements in the form into corresponding variables after the form has been post. This is a poor interaction, which is why you use JavaScript. The reason for using PHP is that it is faster and more secure than other CGI languages, and it is easier to develop.

2. A constantly refreshed chat room
A standard Chat room page consists of three frames, which are say that show online users, user statements and feature configurations, and a list of chat content. The user presses the content in the say frame to send, the data is processed and saved in the MySQL database, and the time of the speaker, the chat object and the speaker is saved. When the user enters the chat room, the List frame displays the statement from the MySQL database that the speaking time is greater than the user's entry time. The key to the subsequent display of the new statement is that the program that displays the chat content is an infinite loop.
Overview of the List frame program:
$db =mysql_pconnect (localhost,root); #Mysql数据库连接
mysql_select_db (chat, $db);
Show Welcome to the chat room
Configure $init to speak time in the database than
The ID number of the first data entering the time is large #是数据提取的标志位
while (1==1) {#无限循环开始
Extracting data from $init to the last;
while (each data $text) {
$emote =strip_tags ($text); #防止用户直接输入Html语言
if (eregi ("^/", $emote)) #判断发言是否系统命令 (with/start)
System Special Handling
Else Display speech
}
Configure $init as the last data ID number
Flush (); #清出输出缓冲, so that the speech immediately showed
Sleep (2); #使程式暂停2秒, Save system resources
Mysql_free_result ($result); #释放数据库结果占用的内存
}
Because the program is infinite loop, the speech output in each loop is first placed in the output buffer. The content of the buffer is sent to the user's list frame by flush (), which achieves the real-time chatting effect. The loop finally releases the memory occupied by the MySQL result set, or the system resources will be exhausted soon because of the infinite loop.
After login, the user will establish a online table for the statistics of users on-line, mainly to prevent the user table is too large, the program frequently used in the retrieval will slow down the operation of the system. One of the most used is the online Frame, which uses the refresh Meta of client pull to query the online table every once in a while to refresh the users on the line. If the user does not speak more than the specified time, the system invokes a custom function to set the user to timeout, forcing it to exit the chat room.

12 Next Page
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.