After using split.py and enable CPPHTTPLIB_ZLIB_SUPPORT, I can't use EventSource.
githubchry opened this issue · 3 comments
githubchry commented
Only when split.py and CPPHTTPLIB_ZLIB_SUPPORT are both applied at the same time...
yhirose commented
@githubchry could you give me more information, and the smallest possible code that can reproduce the problem?
githubchry commented
- split.py => httplib.cc/httplib.h
- Modified based on example/ssesvr.cc
- g++ ssesvr.cpp httplib.cc -lpthread -o ok.out
- g++ ssesvr.cpp httplib.cc -lpthread -DCPPHTTPLIB_ZLIB_SUPPORT -lz -o err.out
- Test ok.out/err.out
err.out did not print event2 log
Here is the source code, with only minor changes: event2 "text/event-stream" => "text/event-stream; charset=utf-8"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include "httplib.h"
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>
using namespace httplib;
using namespace std;
class EventDispatcher {
public:
EventDispatcher() {
}
void wait_event(DataSink *sink) {
unique_lock<mutex> lk(m_);
int id = id_;
cv_.wait(lk, [&] { return cid_ == id; });
sink->write(message_.data(), message_.size());
}
void send_event(const string &message) {
lock_guard<mutex> lk(m_);
cid_ = id_++;
message_ = message;
cv_.notify_all();
}
private:
mutex m_;
condition_variable cv_;
atomic_int id_{0};
atomic_int cid_{-1};
string message_;
};
const auto html = R"(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSE demo</title>
</head>
<body>
dasds
<script>
const ev1 = new EventSource("event1");
ev1.onmessage = function(e) {
console.log('ev1', e.data);
}
const ev2 = new EventSource("event2");
ev2.onmessage = function(e) {
console.log('ev2', e.data);
}
</script>
</body>
</html>
)";
int main(void) {
EventDispatcher ed;
Server svr;
svr.Get("/", [&](const Request & /*req*/, Response &res) {
res.set_content(html, "text/html");
});
svr.Get("/event1", [&](const Request & /*req*/, Response &res) {
cout << "connected to event1..." << endl;
res.set_chunked_content_provider("text/event-stream",
[&](size_t /*offset*/, DataSink &sink) {
ed.wait_event(&sink);
return true;
});
});
svr.Get("/event2", [&](const Request & /*req*/, Response &res) {
cout << "connected to event2..." << endl;
res.set_chunked_content_provider("text/event-stream; charset=utf-8",
[&](size_t /*offset*/, DataSink &sink) {
ed.wait_event(&sink);
return true;
});
});
thread t([&] {
int id = 0;
while (true) {
this_thread::sleep_for(chrono::seconds(1));
cout << "send event: " << id << std::endl;
std::stringstream ss;
ss << "data: " << id << "\n\n";
ed.send_event(ss.str());
id++;
}
});
svr.listen("0.0.0.0", 1234);
}
yhirose commented
@githubchry I tried to reproduce the problem, but it works fine on my machine... There could be something causing this issue in our environment.
![image](https://private-user-images.githubusercontent.com/357397/300199406-53e679c3-0da3-42dd-b7f2-7a6b51374cf5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTA2MDM1NjYsIm5iZiI6MTcxMDYwMzI2NiwicGF0aCI6Ii8zNTczOTcvMzAwMTk5NDA2LTUzZTY3OWMzLTBkYTMtNDJkZC1iN2YyLTdhNmI1MTM3NGNmNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwMzE2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDMxNlQxNTM0MjZaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01Yzg3M2Q3MTY4NGFlNGU0Y2E5Y2Y0MWRkNDVkZjZiYjljODU5N2RkMTQwNzkyMDlhMDYyZjE4NzMyZDgxYWI3JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.RsvUoSfIYXix0zE6V2ZK4nWSciunAHFb3Gca1qHYiuc)