zoukankan      html  css  js  c++  java
  • HTML5与php实现消息推送功能

    1、html页面basic_sse.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>数据推送</title>
    </head>
    <body>
        <pre id="result">
            Initializing...
        </pre>
        <script type="text/javascript">
    
            if(typeof(EventSource)!=="undefined")
              {
              var source = new EventSource("/sjts/index.php");
              source.onmessage=function(event){              
                document.getElementById("result").innerHTML=event.data + "<br />";
                };
              }
            else
              {
              document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
              }
    
        </script>
    </body>
    </html>

    2、php页面index.php

    <?php
        header('Content-Type:text/event-stream');//通知浏览器开启事件推送功能
        header('Cache-Control:no-cache');//告诉浏览器当前页面不进行缓存
    
        //$time = date('r');
        //echo "data: The server time is: {$time}
    
    ";
        
        $mysqli = new MySQLi('localhost','root','','test');
        $sql = 'select ac_id from article_class where ac_name="11";';
        $result = $mysqli->query($sql);
        while($row = $result->fetch_assoc()){
            $time = $row['ac_id'];    
            echo "data: The server time is: {$time}
    
    ";
        }
    
        ob_flush();//刷新
        flush();//刷新
    ?>

    总结:通过改变数据库ac_id自动,前端不刷新即可实施改变数据

    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    黑马程序员_网络编程
    黑马程序员_ 异常
    黑马程序员_面向对象基础
    黑马程序员_循环语句的使用
    黑马程序员_面向对象深入2
    黑马程序员_ JAVA中的多线程
    黑马程序员_JAVA基础知识总结3
    OC-内存管理
    OC-核心语法(3)(分类、SEL、类本质)
    OC-核心语法2-构造方法
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5190155.html
Copyright © 2011-2022 走看看