Skip to content

Commit

Permalink
Improved Stream interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Jan 17, 2020
1 parent e5ca863 commit 2e360f9
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ example/hello
example/simplesvr
example/benchmark
example/redirect
example/sse
example/upload
example/*.pem
test/test
Expand Down
7 changes: 5 additions & 2 deletions example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ OPENSSL_DIR = /usr/local/opt/openssl
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz

all: server client hello simplesvr upload redirect benchmark
all: server client hello simplesvr upload redirect sse benchmark

server : server.cc ../httplib.h Makefile
$(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
Expand All @@ -25,6 +25,9 @@ upload : upload.cc ../httplib.h Makefile
redirect : redirect.cc ../httplib.h Makefile
$(CXX) -o redirect $(CXXFLAGS) redirect.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)

sse : sse.cc ../httplib.h Makefile
$(CXX) -o sse $(CXXFLAGS) sse.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)

benchmark : benchmark.cc ../httplib.h Makefile
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)

Expand All @@ -33,4 +36,4 @@ pem:
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem

clean:
rm server client hello simplesvr upload redirect benchmark *.pem
rm server client hello simplesvr upload redirect sse benchmark *.pem
106 changes: 106 additions & 0 deletions example/sse.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// sse.cc
//
// Copyright (c) 2020 Yuji Hirose. All rights reserved.
// MIT License
//

#include <atomic>
#include <chrono>
#include <condition_variable>
#include <httplib.h>
#include <iostream>
#include <mutex>
#include <thread>

using namespace httplib;
using namespace std;

class EventDispatcher {
public:
EventDispatcher() {
id_ = 0;
cid_ = -1;
}

void add_sink(DataSink *sink) {
unique_lock<mutex> lk(m_);
int id = id_;
cv_.wait(lk, [&] { return cid_ == id; });
if (sink->is_writable()) { 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_;
atomic_int cid_;
string message_;
};

const auto html = R"(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSE demo</title>
</head>
<body>
<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_header("Content-Type", "text/event-stream");
res.set_chunked_content_provider(
[&](uint64_t /*offset*/, DataSink &sink) { ed.add_sink(&sink); });
});

svr.Get("/event2", [&](const Request & /*req*/, Response &res) {
cout << "connected to event2..." << endl;
res.set_header("Content-Type", "text/event-stream");
res.set_chunked_content_provider(
[&](uint64_t /*offset*/, DataSink &sink) { ed.add_sink(&sink); });
});

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("localhost", 1234);
}
Loading

0 comments on commit 2e360f9

Please sign in to comment.