var stream = new pi.comet(name (optional),onread (optional),onclose (optional));
var stream = pi.comet.get(url,onread,onclose);
General Usage:
var stream = new pi.comet;
stream.environment.setName('test');
stream.environment.setUrl('push.php');
stream.event.push = function(_response){ alert(_response); };
stream.event.disconnect = function(){ alert('Connection is closed'); };
stream.environment.send();
There is a one more alternative usage too, you can use the "get" static method:
var stream = pi.comet.get(
'push.php',
function(_response){ alert(response); },
function(){ alert('Connection is closed'); }
);
<button id="start">Start</button><button id="stop">Stop</button> <textarea id="monitor"> </textarea>
#monitor { display:block; width:400px; height:150px; }
var stream;
function start(){
stream = pi.comet.get("examples/simple/simple.php",read,disable);
startButton.attribute.set("disabled","disabled").event.removeListener("click",start);
stopButton.attribute.remove("disabled").event.addListener("click",stop);
textbox.update("");
}
function read(_response){
textbox.element.value+="\n"+_response;
}
function stop(){
stream.abort();
disable();
}
function disable(){
startButton.attribute.remove("disabled").event.addListener("click",start);
stopButton.attribute.set("disabled","disabled").event.removeListener("click",stop);
}
var startButton = new pi.element(pi.get("start")).event.addListener("click",start),
stopButton = new pi.element(pi.get("stop")).attribute.set("disabled","disabled"),
textbox = new pi.element(pi.get("monitor")).attribute.set("readonly","readonly");
<?
require_once "pi_comet.php";
$stream = new PIComet($_GET["PICometMethod"],$_GET["PICometName"]);
header("Content-type:".$stream->header);
$messages = Array("If the number's there I'll find it!","Something's going on.","It has to do with that number.","There's an answer in that number.","You see the simplicity of the circle.","Mathematics is the language of nature","Everything around us can be represented and understood through numbers","If you graph these numbers, patterns emerge.","Therefore: There are patterns everywhere in nature.");
for($i=0; $i<15; $i++){
print $stream->push( time() . " " . $messages[rand(0,count($messages)-1)] );
ob_flush();
flush();
sleep(1);
}
?>