How to implement COMET with PHP
来源:百度文库 编辑:神马文学网 时间:2024/04/28 04:10:16
How to implement COMET with PHP
Comet is a programming technique that enables web servers to send data to the client without having any need for the client to request it. This technique will produce more responsive applications than classic AJAX. In classic AJAX applications, web browser (client) cannot be notified in real time that the server data model has changed. The user must create a request (for example by clicking on a link) or a periodic AJAX request must happen in order to get new data fro the server.
I will now explain how to implement Comet with PHP programming language. I will demonstrate it on two demos which uses two techniques: the first one is based on hidden ”
Comet with iframe technique : server timestamp demo
We need:
- A PHP script that will handle the persistent http request (backend.php)
- A HTML file that will load Javascript code and that will show the data coming from the server (index.html)
- The prototype library that will help us to write simple JS code
To understand how it works this schema should help:
The backend script (PHP)
This script will do an infinite loop and will return the server time as long as the client is connected. Call it “backend.php”:
Comet php backend ';echo 'comet.printServerTime('.time().');';echo '';flush(); // used to send the echoed data to the clientsleep(1); // a little break to unload the server CPU} ?>
The client script (HTML)
This HTML document first load the prototype library in the ”
” tag, then it create the tag that will contains the server timer ””, and finally it create a “comet” javascript object that will connect the backend script to the time container tag.The comet object will create some invisible “iframe” tags (depends on the web browser). These iframes are in charge to create the background persistent http connection with the backend script. Notice: this script do not handle possible connection problems between client and server.
Comet demo The server time will be shown here
Download it
Here is the tar.gz archive of this demo.
Online demo
Here is the online demo.
Comet with classic AJAX : litte chat demo
As on the above technique, we need:
- A file to exchange data (data.txt)
- A PHP script that will handle the persistent http request (backend.php)
- A HTML file that will load Javascript code and that will show the data coming from the server (index.html)
- The prototype library that will help us to write simple JS code
The backend script (PHP)
This script will do two things:
- Write into “data.txt” when new messages are sent
- Do an infinite loop as long as “data.txt” file is unchanged
The client script (HTML)
This HTML document first load the prototype library in the ”
” tag, then it create the tag ”” that will contains the chat messages comming from “data.txt” file, and finally it create a “comet” javascript object that will call the backend script in order to watch for new chat messages.The comet object will send AJAX requests each time a new message has been received and each time a new message is posted. The persistent connection is only used to watch for new messages. A timestamp url parameter is used to identify the last requested message, so that the server will return only when the “data.txt” timestamp is newer that the client timestamp.
Comet demo
Download it
Here is the tar.gz archive of this demo.
Online demo
Here is the online demo.