Push Technology(中文翻譯成"推播技術",用推的...)的技術有很多種(我們是根據Wikipedia研讀的結果),在此是用HTTP的方式實現,因此稱之為HTTP server push,也有另一個說法是HTTP Streaming。
這是一個簡單的範例,使用PHP所撰寫,功能是每間隔1秒鐘丟出XML檔案,若是用Firefox瀏覽這個PHP網頁,你會看到好像時鐘的樣子改變XML的內容。
<?php
header('Content-type: multipart/x-mixed-replace;boundary=endofsection');
date_default_timezone_set("Asia/Taipei");
while (1)
{
sleep(1);//Delay 1 second
print "Content-type: text/xml; Charset=utf-8\n\n";
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement("box");
$doc->appendChild($root);
$time = $doc->createElement("time",date("Y-m-d_H:i:s"));
$root->appendChild($time);
print $doc->saveXML();
print "--endofsection\n";
ob_flush();//Flush (send) the output buffer
flush();//Flush the output buffer
}
?>
這主要是用到MIME(Multipurpose Internet Mail Extensions)的一項內容:multipart/x-mixed-replace,這是定義在RFC 2046規範之中。
沒有留言:
張貼留言