最近一直关注COMET推技术,现在已经应用很普遍了
比如renren,新浪微博,都用到了COMET技术
大部分使用java实现的,下面是用PHP实现的原理
原理:利用htmlfile这个ActiveX,往页面上放一个iframe,设置它的src为请求的地址。
1.我们需要一个持久链接的HTTP请求
2.需要一个加载javascript的html页面
这个脚本会做一个无限循环,将返回服务器的时间,只要客户端连接。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<?php
header(
"Cache-Control: no-cache, must-revalidate"
);
header(
"Expires: Mon, 26 Jul 1997 05:00:00 GMT"
);
flush();
?><!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>
<head>
<title>Comet php backend</title>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
</head>
<body>
<script type=
"text/javascript"
>
// KHTML browser don't share javascripts between iframes
var
is_khtml = navigator.appName.match(
"Konqueror"
) || navigator.appVersion.match(
"KHTML"
);
if
(is_khtml)
{
var
prototypejs = document.createElement('script
');
prototypejs.setAttribute('
type
','
text/javascript
');
prototypejs.setAttribute('
src
','
prototype.js
');
var head = document.getElementsByTagName('
head
');
head[0].appendChild(prototypejs);
}
// load the comet object
var comet = window.parent.comet;
</script>
<?php
while(1) {
echo '
<script type=
"text/javascript"
>
';
echo '
comet.printServerTime(
'.time().'
);
';
echo '
</script>';
flush();
// used to send the echoed data to the client
sleep(1);
// a little break to unload the server CPU
}
?>
</body>
</html>
<BR type=
"_moz"
>
|
创建客户端脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
var
comet = {
connection :
false
,
iframediv :
false
,
initialize:
function
() {
if
(navigator.appVersion.indexOf(
"MSIE"
) != -1) {
// For IE browsers
comet.connection =
new
ActiveXObject(
"htmlfile"
);
comet.connection.open();
comet.connection.write(
"<html>"
);
comet.connection.write(
"<script>document.domain = '"
+document.domain+
"'"
);
comet.connection.write(
"</html>"
);
comet.connection.close();
comet.iframediv = comet.connection.createElement(
"div"
);
comet.connection.appendChild(comet.iframediv);
comet.connection.parentWindow.comet = comet;
comet.iframediv.innerHTML =
"<iframe id='comet_iframe' src='./backend.php'></iframe>"
;
}
else
if
(navigator.appVersion.indexOf(
"KHTML"
) != -1) {
// for KHTML browsers
comet.connection = document.createElement(
'iframe'
);
comet.connection.setAttribute(
'id'
,
'comet_iframe'
);
comet.connection.setAttribute(
'src'
,
'./backend.php'
);
with
(comet.connection.style) {
position =
"absolute"
;
left = top =
"-100px"
;
height = width =
"1px"
;
visibility =
"hidden"
;
}
document.body.appendChild(comet.connection);
}
else
{
// For other browser (Firefox...)
comet.connection = document.createElement(
'iframe'
);
comet.connection.setAttribute(
'id'
,
'comet_iframe'
);
with
(comet.connection.style) {
left = top =
"-100px"
;
height = width =
"1px"
;
visibility =
"hidden"
;
display =
'none'
;
}
comet.iframediv = document.createElement(
'iframe'
);
comet.iframediv.setAttribute(
'src'
,
'./backend.php'
);
comet.connection.appendChild(comet.iframediv);
document.body.appendChild(comet.connection);
}
},
// this function will be called from backend.php
printServerTime:
function
(time) {
$(
'content'
).innerHTML = time;
},
onUnload:
function
() {
if
(comet.connection) {
comet.connection =
false
;
// release the iframe to prevent problems with IE when reloading the page
}
}
}
Event.observe(window,
"load"
, comet.initialize);
Event.observe(window,
"unload"
, comet.onUnload);
|
学习资料:
Comet:基于 HTTP 长连接的“服务器推”技术
http://www.ibm.com/developerworks/cn/web/wa-lo-comet/
How to implement COMET with PHP
http://www.zeitoun.net/articles/comet_and_php/start