• PHP 及时通讯机制代码


    php实现实时通信一般有两种方式:socket或comet。socket是比较好的解决方案,问题在于不是所有的浏览器都兼容,服务器端实现起来也稍微有点麻烦。

    <?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();
    ?>
    觉得有收获,记得推荐一下哦!
    作者:楓羽靈~
    出处:http://520fyl.cnblogs.com/
    如果您觉得本文对您的学习有所帮助,可通过点击页面下方【好文要顶】支持博主。
  • 相关阅读:
    Android 长时间运行任务说明
    phoneGap,angularJs,onSen的一些备忘
    cordova 选择图片并上传到服务器
    手机PC文件传输
    一组RS485设备操作命令
    asp.net 中长尾链接实现推送 -- comet
    ardunio 实现RS485通讯-下位机
    JPEG Camer 图片上传
    ImageResizer 3.4.3配置
    arduino空调遥控器
  • 原文地址:https://www.cnblogs.com/520fyl/p/5396310.html
Copyright © 2020-2023  润新知