简体   繁体   中英

How to combine http requests with SSE (Server SentEvents)

I have a post request that takes a very long time to finish.

I've read all over and got to the conclusion that I need to use SSEs to show the progress. I can see my progress message in the body of the response, if I stop the http request, but I can't seem to manage a way to fetch only the message (apart from the response body).

It seems I need a PHP script to send to the EventSource class in the FE, but all the scripts have 'No direct script access allowed'.

The info is sent from a library, not from the controller itself, the call is invoked by the controller, using index_post.

Is there another way to get my "echoed" messages from the server to show?

I mostly get

 "EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection."

as an error. If I put the headers of the eventstream it doesn't help, because it connects to the index page anyway, I still need it as html.

Again, if you have a better suggestion for me, I'd love to hear.

First - you don't want to access your scripts directly. Using controllers is absolutely ok.

You can send your output to view or echo your output without using views, like this:

echo "data: Hello World!\n\n";
exit;

Your main problem is, that you are not sending proper MIME type. Use output library . Look at the example with $this->output->set_content_type('application/json') and alter it to $this->output->set_content_type('text/event-stream') . You have to put this lines before any output.

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