添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Server sent events - text/event-stream ocasionally misenterpreted as text/html by chrome and firefox

Ask Question

I have built an application that, at some point, uses Server Sent Events, send from a long running php script, to update a progress bar on the frontend. After I have faced the typical problems with flushing the output of the php script and tried all the possible solutions I've read about at SO, I concluded that the only way to make it work was by forcing the output out by echoing a long string of spaces. The function that sends the messages to the browser is the following:

function sendMsg($id, $array) {
            echo "id: $id" . PHP_EOL;
            echo "data: ".json_encode($array).PHP_EOL;
            echo PHP_EOL;
            flush();
            ob_flush();
            echo str_repeat(' ', 4096);

The headers of the script are the following:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

Everything seemed to work great on the PCs I use, but when I tested the application on my sister's laptop I noticed this strange behavior with Firefox and Chrome. After having received the first 2 messages, they drop the connection. Firefox tries to reconnect and Chrome throws an error that says:

EventSource response has a MIME type ("text/html") that is not "text/event-stream"

The versions of the browsers are exactly the same (39.0 and 43.0.2357.134m respectively) in all PCs. My sister's laptop runs Windows 7 Home Premium SP1.

How can this different behavior be explained? Does it depend on the browsers' settings or could it be attributed to some system wide settings? IE 10 on my sister's laptop doesn't present the problem.

Is there a way to debug on the server or find more information on the logs to try to identify if the server is droping the connection? – Ricardo Aug 5, 2015 at 18:58 I wouldn't know how to do that, but i guess that the conection is dropped at client side since it works in every other system i have tried it. By the way, i had to format my sisters laptop for other reasons, and now i don't have any means to experiment. With the new OS (win 10), it works fine. – SilentBob Aug 25, 2015 at 22:52

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.