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!!!
  • 相关阅读:
    loj 6035 「雅礼集训 2017 Day4」洗衣服
    BZOJ 3251 树上三角形
    UwrhrQNgRh
    百度之星2018资格赛1002题解
    [CF-676B]PYRAMID OF GLASSES
    【CF-371C】Hamburgers
    洛谷P1012拼数——字符串排序
    位运算详解及应用
    NOIP 2014 Day2 T1 无线网络发射器
    写代码要注意的几点(2)
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5190155.html
Copyright © 2011-2022 走看看