js的comet各个浏览器封装lib
生活随笔
收集整理的這篇文章主要介紹了
js的comet各个浏览器封装lib
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? SimpleComet是一個(gè)輕量級(jí)的comet的封裝類, 能智能的識(shí)別瀏覽器,應(yīng)用不同的comet技術(shù),在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能監(jiān)聽(tīng)出鏈接是否已經(jīng)斷了,是不是很智能,只要負(fù)責(zé)服務(wù)端的輸出就行.?
下載:?http://www.mandor.net/files/simplecomet-1.0.zip
轉(zhuǎn)載于:https://www.cnblogs.com/torome/archive/2011/03/10/1979944.html
總結(jié)
以上是生活随笔為你收集整理的js的comet各个浏览器封装lib的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: TweetLouder.com:在微博上
- 下一篇: 赶集网人事调整:三月内两副总离职