2019-05-05 00:11:57 -07:00
|
|
|
#include "stream.h"
|
|
|
|
|
|
|
|
|
|
namespace firesse {
|
|
|
|
|
|
2019-05-07 23:26:48 -07:00
|
|
|
Stream::Stream(firecgi::Request* request)
|
2019-05-11 21:53:12 -07:00
|
|
|
: request_(request) {
|
|
|
|
|
request->OnClose([this]() {
|
|
|
|
|
if (on_close_) {
|
|
|
|
|
on_close_();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Stream::OnClose(const std::function<void()>& callback) {
|
|
|
|
|
on_close_ = callback;
|
|
|
|
|
}
|
2019-05-05 00:11:57 -07:00
|
|
|
|
|
|
|
|
bool Stream::WriteEvent(const std::string& data, uint64_t id, const std::string& type) {
|
2019-05-09 23:37:00 -07:00
|
|
|
return request_->InTransaction<bool>([=]() {
|
|
|
|
|
if (id) {
|
|
|
|
|
request_->WriteBody("id: ", std::to_string(id), "\n");
|
|
|
|
|
}
|
|
|
|
|
if (!type.empty()) {
|
|
|
|
|
request_->WriteBody("event: ", type, "\n");
|
|
|
|
|
}
|
|
|
|
|
request_->WriteBody("data: ", data, "\n\n");
|
|
|
|
|
return request_->Flush();
|
|
|
|
|
});
|
2019-05-05 00:11:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Stream::End() {
|
|
|
|
|
return request_->End();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace firesse
|