artyom-beilis / cppcms

CppCMS Framework
Other
443 stars 107 forks source link

Server Sent Events implementation #85

Closed masaoliou closed 3 years ago

masaoliou commented 3 years ago

This application runs with lighttpd and is mounted as cppcms::app::asynchronous.

Client never gets the message.

If this is because the output is not completely flushed, how do I manually force it out? If this is because I need to append an end-of-message indicator to the end of the outbound message, how do I implement it?

void server_sent_event::send_to_client(booster::shared_ptr<cppcms::http::context> context,unsigned type,const std::string &message)
{
    context->response().set_header("Content-Type","text/event-stream");
    context->response().add_header("Cache-Control","no-cache");
    context->response().out() << "event:" << type << "\n" << "data:" << message << "\n\n";
    context->async_complete_response(); //EDIT: This call does force out the message to client, but it also close the connection. I want the connection to be persistent.
    context->async_flush_output(
        [](cppcms::http::context::completion_type result){
            BOOSTER_DEBUG("send") << result; //always "0"
        }
    );
}
masaoliou commented 3 years ago

This issue is solved thanks to this.