简体   繁体   中英

HTML5 Server-Sent Events : empty response

I am writing a web service using PHP (CodeIgniter). I tried to follow several basic tutorials on Server-Sent Events in HTML5.

However, I wasn't able to run any of them.
I am receiving proper headers, as expected (every 3 seconds), but I don't receive any data.
Could it be a WAMP configuration or CodeIgniter problem?


The code of the controller method:

public function sse() {
        header('Content-Type: text/event-stream');
        header('Cache-Control: no-cache');
        $time = date('r');
        echo "data: The server time is: {$time}\n\n";
        flush();
    }

The code of the view (partially):

<div id="play"> msg </div>

<script>
    var source = new EventSource("<?php echo site_url('play/sse') ?>");
    source.onmessage=function(event) {
        document.getElementById("play").innerHTML+=event.data + "<br>";
    };
</script>

HTTP headers :

Request URL:http://localhost/index.php/play/sse
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/event-stream
Accept-Encoding:gzip,deflate,sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Cookie: ...

I don't know, why it does not works for you, but I had very similar issue.

I had one line response with everything "correct". But, I did not expected, that even one line event-stream requires the \\n\\n on end.

Your sse example is in function. Have you even ran it?

I had the same problem and i solved it by replacing the single quotes with double quote in header and in response/results.

   header("Content-type: text/event-stream");
   header("Connection: Keep-alive");
   header("Cache-Control: no-cache");
   echo "retry:1000\n";
   $result = "data:".json_encode($result)."\n\n"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM