js的comet各个浏览器封装lib
SimpleComet是一个轻量级的comet的封装类, 能智能的识别浏览器,应用不同的comet技术,在firefox浏览器上使用ajax,在IE、opera浏览器上使用htmlfile,iframe.
<script type="text/javascript"> // This function will be called every time the server pushes a new event. function push(event) { // For this example, we simply show the excuse on the page. document.getElementById('excuse').innerHTML = event; } // This function will be called when/if the stream closes. function disconnected() { // For this example we'll just show a nice message. document.getElementById('excuse').innerHTML = '<img src="img/arrow.png" alt="" title="" />'+ ' Click to see MORE reasons why this souldn\'t work!'; document.getElementById('control').value = 'start'; } // This function is executed when the button is clicked. function toggle() { // First we check if the stream is open. if (!comet.active) { // Lets start streaming! comet.open('excuses.php', push, disconnected); document.getElementById('control').value = 'stop'; } else { // Streaming is active, means the user wants to stop it. comet.close(); } } </script>
<!--?php // First we load the SimpleComet PHP class and the list of excuses. require('inc/simplecomet.class.php'); $comet = new SimpleComet(); $excuses = file('inc/excuses.txt'); // This is an infinite loop, which makes the stream endless. while (true) { // We fetch an excuse at random. $excuse = trim($excuses[rand(0, count($excuses))]); // If the excuse is too long, we get another one. if (strlen($excuse) --> 60) { continue; } // Finally, we push our excuse to the client. $comet->push($excuse); // 5 seconds delay before the next excuse. sleep(5); } ?>
simplecomet能监听出链接是否已经断了,是不是很智能,只要负责服务端的输出就行.
下载: http://www.mandor.net/files/simplecomet-1.0.zip