Comments (3)
@githubchry could you give me more information, and the smallest possible code that can reproduce the problem?
from cpp-httplib.
- 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);
}
from cpp-httplib.
Related Issues (20)
- Behaviour when number of threads are exceeded. HOT 2
- How to access the Response object from within the TaskQueue. HOT 4
- Hopefully a websocket service can be added HOT 2
- How to detect client timeouts/disconnects when implementing long polling HOT 1
- SSL Connection Failed HOT 2
- Segmentation Failed on client side only? HOT 2
- Can't disable CORS policy for POST Server handler HOT 1
- does the community weclome a new blood?/(ćoć)/~~ HOT 1
- Server process doesn't get killed when window that is running process gets closed HOT 2
- Client timeout HOT 4
- Crash on httplib post method triggered by malloc.c HOT 8
- What about current version of library ? HOT 1
- When making a form-data request using OkHttp, a status code of 400 is returned. HOT 1
- Server::set_payload_max_length cannot increase the payload size beyond 8 KiB HOT 4
- POST with no POST data returns HTTP 400 HOT 13
- post multipart/form-data errror HOT 1
- usage of rand() is not seeded and depends on seeding by parent program HOT 10
- can't communicate within local network HOT 1
- Doesn't work containerized HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpp-httplib.