PHP server actively sends data to the client solution

WBOY
Release: 2016-07-29 09:06:01
Original
1449 people have browsed it

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++;
        }
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!