Re: [Cppcms-users] "endless" stream of data
Brought to you by:
artyom-beilis
From: Marcel H. <ke...@co...> - 2011-12-27 19:34:22
|
Alright, my code is a bit different from yours and from the chat example but else it doesn't work. void SSE::loop(std::string /*page*/) { response().io_mode(cppcms::http::response::asynchronous); response().set_header("Content-Type", "text/event-stream"); int counter = (rand() % 10) + 1; while (true) { response().out() << "event: ping\n" << "data: {\"time\": \"" << "foo" //note: this is just an example << "\"}\n\n"; counter--; if (!counter) { response().out() << "data: is a message at time " << "bar" << "\n\n"; counter = (rand() % 10) + 1; } context().async_flush_output(boost::bind(&SSE::handler, this, -1)); booster::ptime::sleep(booster::ptime::seconds(1)); } } void SSE::handler(int status) { std::cout << "Status: " << status << std::endl; } two major questions. You told me to use release_context but if I do so, the connection "aborts" (access to unassigned context) while the second loop on response().out() How can I figure out when the user "leaves" the page, so I can stop producing tons of messages? Every connection should get it's own messagestream (it's user dependent). |