php 實現推技術comet(轉)


實現實時通信一般有兩種方式:
socket或comet。socket是比較好的解決方案,問題在於不是所有的瀏覽器都兼容,服務器端實現起來也稍微有點麻煩。相比之下,comet(基於HTTP長連接的"服務器推")實現起來更加方便,而且兼容所有的瀏覽器。所以這次就來說說comet的php實現。

comet也有好幾種實現方式,如iframe, http long request,本文主要探討http long request實現實時通信。

先說說http長鏈接是怎么回事,通俗點講就是服務器不是一收到請求就直接吐數據,而是在那憋啊憋,一直憋到憋不住了,才告訴你執行結果。

至於憋多長時間,就看具體應用了,如果憋太久的話,服務器資源的占用也會是個問題。

現在我們就要通過這種方法來實現實時通信(其實是准實時),先說一下原理:

1. 客戶端發起一個ajax長鏈接查詢,然后服務端就開始執行代碼,主要是檢查某個文件是否被更新,如果沒有,睡一會(sleep),醒來接着檢查
2. 如果客戶端又發起了一個查詢鏈接(正常請求),服務端收到后,處理請求,處理完畢后更新某個特定文件的modify time
3. 這時第一次ajax查詢的后台代碼還在執行,發現某個文件被更新,說明來了新請求,輸出對應的結果
4. 第一次ajax查詢的callback被觸發,更新頁面,然后再發起一個新的ajax長鏈接

<?php
// NovComet.php
class NovComet {
    const COMET_OK = 0;
    const COMET_CHANGED = 1;

    private $_tries;
    private $_var;
    private $_sleep;
    private $_ids = array();
    private $_callback = null;

    public function  __construct($tries = 20, $sleep = 2)
    {
        $this->_tries = $tries;
        $this->_sleep = $sleep;
    }

    public function setVar($key, $value)
    {
        $this->_vars[$key] = $value;
    }

    public function setTries($tries)
    {
        $this->_tries = $tries;
    }

    public function setSleepTime($sleep)
    {
        $this->_sleep = $sleep;
    }

    public function setCallbackCheck($callback)
    {
        $this->_callback = $callback;
    }

    const DEFAULT_COMET_PATH = "/dev/shm/%s.comet";

    public function run() {
        if (is_null($this->_callback)) {
            $defaultCometPAth = self::DEFAULT_COMET_PATH;
            $callback = function($id) use ($defaultCometPAth) {
                $cometFile = sprintf($defaultCometPAth, $id);
                return (is_file($cometFile)) ? filemtime($cometFile) : 0;
            };
        } else {
            $callback = $this->_callback;
        }

        for ($i = 0; $i < $this->_tries; $i++) {
            foreach ($this->_vars as $id => $timestamp) {
                if ((integer) $timestamp == 0) {
                    $timestamp = time();
                }
                $fileTimestamp = $callback($id);
                if ($fileTimestamp > $timestamp) {
                    $out[$id] = $fileTimestamp;
                }
                clearstatcache();
            }
            if (count($out) > 0) {
                return json_encode(array('s' => self::COMET_CHANGED, 'k' => $out));
            }
            sleep($this->_sleep);
        }
        return json_encode(array('s' => self::COMET_OK));
    }

    public function publish($id)
    {
        return json_encode(touch(sprintf(self::DEFAULT_COMET_PATH, $id)));
    }
}
<?php
// comet.php
include('NovComet.php');

$comet = new NovComet();
$publish = filter_input(INPUT_GET, 'publish', FILTER_SANITIZE_STRING);
if ($publish != '') {
    echo $comet->publish($publish);
} else {
    foreach (filter_var_array($_GET['subscribed'], FILTER_SANITIZE_NUMBER_INT) as $key => $value) {
        $comet->setVar($key, $value);
    }
    echo $comet->run();
}
 
    function send(msg){
        $.ajax({
            data : {'msg' : msg},
            type : 'post',
            url : '{:U('Live/SendMsg')}',
            success : function(response){
               //alert(response);;
            }
        })
    }
    $(document).ready(function(){
        connect();
        $("#btn").click(function(){
            var msg = $('#msg').val();
            send(msg);
            msg.html('');
          });
    })
    public function SendMsg(){
        
        $filename  = './Uploads/live/'.'data.json';
        if ($_POST['msg']!='') {
            file_put_contents($filename,$_POST['msg']);
            $this->ajaxReturn($_POST,'OK',100);
            die();
        }else{
            $this->ajaxReturn($_POST,'on',0);
            die();
        }
        
    }
 
         

 

 
<!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>Comet demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="./jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="./json2.js"></script>
<script>
    var timestamp = 0;
    var url = 'backend.php';
    var error = false;
    // 通過ajax建立和php端處理函數的連接(通過遞歸調用建立長時間的連接)
    function connect(){
        $.ajax({
            data : {'timestamp' : timestamp},
            url : url,
            type : 'get',
            timeout : 0,
            success : function(response){
                var data = JSON.parse(response);
                error = false;
                timestamp = data.timestamp;
                if (data.msg != undefined && data.msg != "")
                {
                    $("#content").append("<div>" + data.msg + "</div>");
                }
            },
            error : function(){
                error = true;
                setTimeout(function(){ connect();}, 5000);
            },
            complete : function(){
                if (error)
                    // 請求有錯誤時,延遲5s再連接
                    setTimeout(function(){connect();}, 5000);
                else
                    connect();
            }
        })
    }
    // 發送信息
    function send(msg){
        $.ajax({
            data : {'msg' : msg},
            type : 'get',
            url : url
        })
    }
    // 創建長時間的連接
    $(document).ready(function(){
        connect();
    })
</script>
</head>
<body>
    <div id="content"></div>
        <form action="" method="get" 
onsubmit="send($('#word').val());$('#word').val('');return false;">
            <input type="text" name="word" id="word" value="" />
            <input type="submit" name="submit" value="Send" />
        </form>
    </body>
</html>
<?php
// 設置請求運行時間不限制,解決因為超過服務器運行時間而結束請求
ini_set("max_execution_time", "0");

$filename  = dirname(__FILE__).'/data.txt';
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';

// 判斷頁面提交過來的修改內容是否為空,不為空則將內容寫入文件,並中斷流程
if ($msg != '')
{
    file_put_contents($filename,$msg);
    exit;
}

/* 獲取文件上次修改時間戳 和 當前獲取到的最近一次文件修改時間戳
 * 文件上次修改時間戳 初始 默認值為0
 * 最近一次文件修改時間戳 通過 函數 filemtime()獲取
 */
$lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
clearstatcache();  // 清除文件狀態緩存
$currentmodif = filemtime($filename);

/* 如果當前返回的文件修改unix時間戳小於或等於上次的修改時間,
 * 表明文件沒有更新不需要推送消息
 * 如果當前返回的文件修改unix時間戳大於上次的修改時間
 * 表明文件有更新需要輸出修改的內容作為推送消息
 */
while ($currentmodif <= $lastmodif)
{
    usleep(10000);     // 休眠10ms釋放cpu的占用
    clearstatcache();  // 清除文件狀態緩存
    $currentmodif = filemtime($filename);
}

// 推送信息處理(需要推送說明文件有更改,推送信息包含本次修改時間、內容)
$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
?>

最后,話說,php真不適合干這個,我覺得用nodejs 寫是最輕松的,erlang好像也不錯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM