You've already forked cpp-httplib
Implement SSEClient
This commit is contained in:
@@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
|
||||
BROTLI_DIR = $(PREFIX)/opt/brotli
|
||||
BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
||||
|
||||
all: server client hello simplecli simplesvr upload redirect ssesvr ssecli ssecli-stream benchmark one_time_request server_and_client accept_header
|
||||
all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header
|
||||
|
||||
server : server.cc ../httplib.h Makefile
|
||||
$(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
@@ -47,9 +47,6 @@ ssesvr : ssesvr.cc ../httplib.h Makefile
|
||||
ssecli : ssecli.cc ../httplib.h Makefile
|
||||
$(CXX) -o ssecli $(CXXFLAGS) ssecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
ssecli-stream : ssecli-stream.cc ../httplib.h ../httplib.h Makefile
|
||||
$(CXX) -o ssecli-stream $(CXXFLAGS) ssecli-stream.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
benchmark : benchmark.cc ../httplib.h Makefile
|
||||
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
@@ -67,4 +64,4 @@ pem:
|
||||
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
||||
|
||||
clean:
|
||||
rm server client hello simplecli simplesvr upload redirect ssesvr ssecli ssecli-stream benchmark one_time_request server_and_client accept_header *.pem
|
||||
rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header *.pem
|
||||
|
||||
@@ -6,16 +6,52 @@
|
||||
//
|
||||
|
||||
#include <httplib.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
httplib::Client("http://localhost:1234")
|
||||
.Get("/event1", [&](const char *data, size_t data_length) {
|
||||
std::cout << string(data, data_length);
|
||||
return true;
|
||||
});
|
||||
// Global SSEClient pointer for signal handling
|
||||
httplib::sse::SSEClient *g_sse = nullptr;
|
||||
|
||||
void signal_handler(int) {
|
||||
if (g_sse) { g_sse->stop(); }
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// Configuration
|
||||
const string host = "http://localhost:1234";
|
||||
const string path = "/event1";
|
||||
|
||||
cout << "SSE Client using httplib::sse::SSEClient\n";
|
||||
cout << "Connecting to: " << host << path << "\n";
|
||||
cout << "Press Ctrl+C to exit\n\n";
|
||||
|
||||
httplib::Client cli(host);
|
||||
httplib::sse::SSEClient sse(cli, path);
|
||||
|
||||
// Set up signal handler for graceful shutdown
|
||||
g_sse = &sse;
|
||||
signal(SIGINT, signal_handler);
|
||||
|
||||
// Event handlers
|
||||
sse.on_open([]() { cout << "[Connected]\n\n"; });
|
||||
|
||||
sse.on_message([](const httplib::sse::SSEMessage &msg) {
|
||||
cout << "Event: " << msg.event << "\n";
|
||||
cout << "Data: " << msg.data << "\n";
|
||||
if (!msg.id.empty()) { cout << "ID: " << msg.id << "\n"; }
|
||||
cout << "\n";
|
||||
});
|
||||
|
||||
sse.on_error([](httplib::Error err) {
|
||||
cerr << "[Error] " << httplib::to_string(err) << "\n";
|
||||
});
|
||||
|
||||
// Start with auto-reconnect (blocking)
|
||||
sse.start();
|
||||
|
||||
cout << "\n[Disconnected]\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user