PHP chat room technology, read PHP chat room technology, PHP chat room Technology Huang Guohui 1. Preface online chat is currently the most popular way to make friends. Chat rooms launched by various websites have their own characteristics. Chat rooms are divided into WebChat and BBSChat. BBSChat is a Telnet-based Tcp protocol. it is a function attached to BBS and requires a client Telnet program. For WebChat, "> <LINKh
PHP chat room technology
Huang Guohui
1. Preface
Online chat is the most popular way to make friends. Chat rooms launched by various websites have their own characteristics.
Chat rooms are divided into WebChat and BBSChat. BBSChat is a Telnet-based Tcp protocol. it is a function attached to BBS and requires a client Telnet program. WebChat adopts a browser, which is actually a CGI program used by many people. The basic principle is to send each user's speech to the system through a browser, and then the system collects and processes the speech and delivers it to specific users.
WebChat generally uses Server Push or Client Pull technology. The difference between the two technologies is that different methods are used to distribute data to users. Server Push means that the Server uses multi-MIME encoding to push data to (Push) users, currently, few websites use this method. The Client Pull is the data that the user pulls from the server (pull.
The most common Client Pull is to use the Meta tag http-equiv = "Refresh" in the Html language to check whether new data exists on the server at regular intervals. For example, the page is refreshed every five seconds. This method is simple and effective. The disadvantage is that it will flash during refresh. in order to maintain efficiency, the old chat content will be cleared every time you refresh the page, it is inconvenient for users to view or retain the conversation content. Therefore, using JavaApplet as the front-end of the chat room, and using refresh to display Pull data from the server through JavaApplet is also a solution. This article also describes how to keep chat programs connected. Method 1: configure the chat program to be infinitely large, so that the browser can keep the download link continuously. Method 2: There is an infinite loop in the chat program, because more advanced functions can be conveniently configured, you can choose to use them here.
The Web server uses FreeBSD + Apache because the combination of FreeBSD and Apache has the strongest performance, and the cost is zero. You need to consider how to store data. It is relatively easy to use documents, but it is often used by many people to perform IO operations on the same document, which will inevitably affect the efficiency. Moreover, FreeBSD's IO performance is not good. Consider using RamDisk to put the entire document into the memory to increase the speed. Or a high-speed space is allocated in the memory for data storage. The author uses the database method: MySQL. Because the database is designed for the simultaneous use of a large number of users, it can be used to save the complicated compilation of high-speed space operations and ensure the speed. If the entire database is put into the memory, the effect is better.
The program is written in PHP + Html + JavaScript. The chat room is mainly used to operate various elements in the Html Form. JavaScript is an object-based language that treats all elements in Html as objects. Therefore, each element has rich methods and attributes and is easy to operate. PHP can process user input data only after the Form is Post and the elements in the Form are converted into corresponding variables. Poor interactivity, which is also the reason for using JavaScript. The reason for using PHP is that it is faster and safer than other CGI languages, and R & D is easier.
2. refresh chat rooms
A standard chat room page consists of three frames, which display Online user Online, user speech, function configuration, and chat content List. The user typed in the speech content in the Say Frame and sent it. The data was processed and stored in the Mysql database. at the same time, the speaker, chat object, and time of the speech were saved. As soon as the user enters the chat room, the List Frame will display the comments whose speaking time is later than the user's entering time from the Mysql database. The key to displaying new speech content in the future is that the program that displays the chat content is infinite loop.
List Frame Program Overview:
$ Db = mysql_pconnect (localhost, root); # Mysql database connection
Mysql_select_db (chat, $ db );
Show welcome to chat room
Configure $ init as the speaking time ratio in the database
Enter the ID of the first data with a large time # It is the identifier of data extraction
While (1 = 1) {# start of an infinite loop
Extract data from $ init to the end;
While ($ text for each data ){
$ Emote = strip_tags ($ text); # prevent users from directly entering the Html language
If (eregi ("^/", $ emote) # judge whether the speech is a system command (starts)
System Special handling
Else presentation
}
Configure $ init as the ID of the last data
Flush (); # clear the output buffer so that the speech is displayed immediately
Sleep (2); # pause the program for 2 seconds to save system resources
Mysql_free_result ($ result); # release the memory occupied by the database results
}
Because the program has infinite loops, the output speech in each loop is first placed in the output buffer. Uses flush () to immediately send the buffer content to the user's List Frame, achieving real-time chat. At the end of the loop, the memory occupied by the Mysql result set will be released. otherwise, system resources will soon be exhausted due to the infinite loop.
After Login, you can create an Online table for Online User statistics. The main purpose is to prevent the User table from being too large. the frequently used search in the program will slow down the system operation. Among them, Online Frame is the most used. when Client Pull's Refresh Meta is used, Online tables are queried at intervals to Refresh Online users. If the user does not speak for more than the specified time, the system will call the custom function to set the user to TimeOut and force the user to exit the chat room.