Google Chrome and Streaming HTTP connections?

2019-01-25 22:19发布

问题:

Google chrome doesn't behave the same as other browsers when encountering this nugget:

<?php
while (true) {
    echo "<script type='text/javascript'>\n";
    echo "alert('hello');\n";
    echo "</script>";
    flush();

    sleep(5);
}
?>

It seems that it's waiting for the connection to terminate before doing anything.

Other than polling how can I do a similar thing in Google Chrome?

回答1:

Some browsers require a certain number of bytes to be downloaded before rendering available data. I remember the last time I tried to do what you're doing I ended up having to dump something like 300 spaces to be sure the browser would bother with it.



回答2:

I had a similar issue to this, and solved it by adding an HTML tag (in my case <br />) before each flush.

My guess would be that Chrome waits for an element which is being displayed to close before triggering a re-render. That's only a guess though.

It didn't seem to require 1024 bytes - I think I would have had just under 512 bytes when it worked.



回答3:

I wish I had access to Chrome at the moment to test out some ideas. Have you tried adding some HTML after </script> and seeing if it renders incrementally? I imagine it would, and if so that'd be proof that Chrome doesn't want to run javascript in <script> elements while the page is loading. Of course, rendering the markup might trigger your scripts to run. If not, you could try including the javascript as external files and see if that affects execution time.

I think browsers generally have some leeway according to the spec in when they begin executing javascript, especially as the page loads. It might not be possible to do this in a fully cross-browser way without polling.



回答4:

Did you talk with Chrome developers? Did you open a bug about that? IMHO the best solution is make Chrome behave like other browsers do, rather than having a workaround for it.

Okay, actually you probably will need a short-term workaround. But imagine a world in which each browser behaves differently in each aspect, say HTTP, HTML, CSS handling... it would not be a pleasant place!



回答5:

Stream is working. The answer from eyelidlessness is the solution.

print "2048 points[BR>\n";

The [ = <

BTW look at the user-agent. Safari needs much bytes too. I think 1024. Firefox needs not so much bytes.



回答6:

<?php
$i = 0;
while (true) {
    if($i == 0) {
        echo "<html><body>";
    }
    echo "<script type='text/javascript'>\n";
        echo "alert('hello');\n";
    echo "</script>";
    if($i == 0 ) {
        $padstr = str_pad("",2048,"&nbsp;");
        echo $padstr;
        echo "</body></html>";
    }
    flush();

    sleep(5);
    $i = $i + 1;
}
?>

For fist time send at least 2048 bytes of data. then it will work fine. And make sure to keep script tag in a body tag. The strange thing is , in my case if I add 1024 bytes it worked. Hope this helps you

The above program is working fine in google chrome.