Home  >  Article  >  Backend Development  >  PHP server actively sends data to the client solution

PHP server actively sends data to the client solution

WBOY
WBOYOriginal
2016-07-29 09:06:011481browse

1. Use a hidden iframe on the client, and its src points to the control code on the server, such as server.php

2. In server.php, use while to implement a never-ending request, and within the loop, implement breakpoints, such as sleep( 2), which means looping every 2 seconds

3. Output the fragment code to the client in each loop

echo "<script>parent.$('#list').append('<li> xxxxxxxxx</li>')</script>";

ob_flush();

flush();

The overall server-side test code is implemented as follows:

        $i = 0;
        while ($i < 20){
            echo '<script>
                parent.$(\'.contentlist\').append("<div>xxxxxxxxxxxxx</div>");
            </script>';
            ob_flush();
            flush();
            sleep(2);
            $i++;
        }

The above introduces the solution for the PHP server to actively send data to the client, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn