页面更新时,心跳刷新浏览器

时间:2018-02-20 13:25:33

标签: php wordpress api heartbeat

我正在尝试在后端更新页面时自动强制在特定页面上刷新浏览器。我试图通过使用Heartbeat API来实现。

我查看了所有可以找到的Heartbeat示例,但我无法掌握它的整个概念。

我能做的就是在这个特定页面上每隔15秒就调出一个字符串。

到目前为止我有什么:

https://pastebin.com/ELQ9uJAw

<?PHP  
//embed heartbeat api
    function heartbeat_test_enqueue($hook_suffix) {
            if ( is_page(1105)) {
                // Make sure the JS part of the Heartbeat API is loaded.
                wp_enqueue_script('heartbeat');

                // Output the test JS in footer.
                add_action( 'print_footer_scripts', 'heartbeat_test_js', 20 );

                //Add filter to receive hook, and specify we need 2 parameters.
                add_filter( 'heartbeat_received', 'dw_receive_heartbeat', 10, 2 );
            }

    }
    add_action( 'wp_enqueue_scripts', 'heartbeat_test_enqueue' );


    //clientside
    function heartbeat_test_js() {
            ?>
            <script>

            jQuery(document).ready( function($) {
                    // Hook into the heartbeat-send
                    jQuery( document ).on( 'heartbeat-send', function( e, data ) {
                        //
                    });

                    // Listen for the custom event "heartbeat-tick" on $(document). This fire's once every minute that the page is open.
                    jQuery(document).on( 'heartbeat-tick', function(e, data) {
                        console.log("tick");
                    });
            });

            </script>
            <?php
    }


    //change heartbeat interval time
    function m_heartbeat_settings( $settings ) {
       $settings['interval'] = 15; //only values between 15 and 60 seconds allowed
       return $settings;
    }
    add_filter( 'heartbeat_settings', 'm_heartbeat_settings' );

    //heartbeat api end
?>

我想使用&#34; post_updated&#34;挂钩检查&#34; post_date&#34;已经改变。但我不知道如何将此钩子与Heartbeat API结合使用。

先谢谢,我真的迷失了。

1 个答案:

答案 0 :(得分:0)

终于找到了办法。

//embed heartbeat api
function heartbeat_test_enqueue($hook_suffix) {
        if ( is_page(1105)) {
            // Make sure the JS part of the Heartbeat API is loaded.
            wp_enqueue_script('heartbeat');

            // Output the test JS in admin footer.
            add_action( 'print_footer_scripts', 'heartbeat_update', 20 ); 

            //Add filter to receive hook, and specify we need 2 parameters.
            add_filter( 'heartbeat_received', 'dw_receive_heartbeat', 10, 2 );

        }

}
add_action( 'wp_enqueue_scripts', 'heartbeat_test_enqueue' );

//write timestamp file
function write_timestamp($post_ID, $post_after, $post_before){
    if ( $post_ID == 1105 ) {
        $myfile = fopen("URLtoTextFile/tv.txt", "w") or die("Unable to open file!");
        $txt = $post_after->post_modified;
        fwrite($myfile, $txt);
        fclose($myfile);
    }
}
add_action( 'post_updated', 'write_timestamp', 10, 3 );

//clientside
function heartbeat_update() {
        ?>
        <script>

        jQuery(document).ready( function($) {

                var current_timestamp;

                jQuery.ajax({
                    url: "/tv.txt",
                    cache: false,
                    success: function (data){
                        current_timestamp = data;
                }});

                // Listen for the custom event "heartbeat-tick" on $(document). This fire's once every 15s that the page is open.
                jQuery(document).on( 'heartbeat-tick', function(e, data) {              
                    jQuery.ajax({
                        url: "/tv.txt",
                        cache: false,
                        success: function (data){
                            if (data != current_timestamp) {
                                jQuery('body').fadeOut(1000, function(){
                                    location.reload(true);
                                });
                            }
                        }
                    });
                });
        });

        </script>
        <?php
}



//change heartbeat interval time
function m_heartbeat_settings( $settings ) {
   $settings['interval'] = 15; //only values between 15 and 60 seconds allowed
   return $settings;
}
add_filter( 'heartbeat_settings', 'm_heartbeat_settings' );

//heartbeat api end 

我在做什么:

我在.txt文件中写了页面的修改日期,每次更新后我都挂钩到post_updated钩子并检查日期是否发生了变化。然后我只用jQuery刷新浏览器。