Here's some code I came up with for COMET based stuff. It has ways of
dealing with memory usage!

index.html
========
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/
TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml";>
 <head>
  <title>jQuery Comet demo</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.2.6/jquery.min.js"></script>
 </head>
 <body>
 <div id="comet_frame" style="display:none;visablity:invisable;"></
div>
 <div id="content"></div>
 <input type="button" id="newframe" value="add"><input type="button"
id="removeframe" value="remove">
 <script type="text/javascript">
  var comet =
   {
    load: function()
     {
      $("#comet_frame").html('<iframe id="comet_iframe"
src="backend.php"></iframe>');
     },

    unload: function()
     {
      $("#comet_frame").html('<iframe id="comet_iframe" src=""></
iframe>');
     },
    clearFrame: function()
     {
      $("#comet_iframe").html("");
     },
    time: function(ctime)
     {
      $("#content").html(ctime);
     }
   }

 $(document).ready(function()
   {
    $("#newframe").click(function(){comet.load();});
    $("#removeframe").click(function(){comet.unload();});
   });
 </script>
 <body>

backend.php
=========
<?php
function flushHard()
 {
  // echo an extra 256 byte to the browswer - Fix for IE.
  for($i=1;$i<=256;++$i)
   {
    echo ' ';
   }
  flush();
  ob_flush();
 }

set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
flushHard();

$x = 0;
while(1)
 {
  if($x == 10)
   {
    echo '<script type="text/javascript">parent.comet.clearFrame();</
script>'."\n";
   }
  else
   {
    echo '<script type="text/
javascript">parent.comet.time("'.date('H:i:s').'");</script>'."\n";
   }
  flushHard();
  ++$x;
  sleep(1);
 }

Reply via email to