SSE client example

This commit is contained in:
yhirose 2020-07-25 11:20:57 -04:00
parent 90da199aba
commit 0e9cfd9f49
5 changed files with 42 additions and 20 deletions

2
.gitignore vendored
View file

@ -7,7 +7,7 @@ example/simplecli
example/simplesvr
example/benchmark
example/redirect
example/sse
example/sse*
example/upload
example/*.pem
test/test

View file

@ -243,7 +243,9 @@ svr.set_payload_max_length(1024 * 1024 * 512); // 512MB
### Server-Sent Events
Please check [here](https://github.com/yhirose/cpp-httplib/blob/master/example/sse.cc).
[Server example](https://github.com/yhirose/cpp-httplib/blob/master/example/ssesvr.cc)
[Client example](https://github.com/yhirose/cpp-httplib/blob/master/example/ssecli.cc)
### Default thread pool support
@ -306,20 +308,6 @@ httplib::Headers headers = {
auto res = cli.Get("/hi", headers);
```
### GET with Content Receiver
```c++
std::string body;
auto res = cli.Get("/large-data",
[&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
assert(res->body.empty());
```
### POST
```c++
@ -390,6 +378,16 @@ cli.set_write_timeout(5, 0); // 5 seconds
### Receive content with Content receiver
```c++
std::string body;
auto res = cli.Get("/large-data",
[&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
```
```cpp
std::string body;
auto res = cli.Get(

View file

@ -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 simplecli simplesvr upload redirect sse benchmark
all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark
server : server.cc ../httplib.h Makefile
$(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
@ -28,8 +28,11 @@ 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)
ssesvr : ssesvr.cc ../httplib.h Makefile
$(CXX) -o ssesvr $(CXXFLAGS) ssesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
ssecli : ssecli.cc ../httplib.h Makefile
$(CXX) -o ssecli $(CXXFLAGS) ssecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
benchmark : benchmark.cc ../httplib.h Makefile
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
@ -39,4 +42,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 sse benchmark *.pem
rm server client hello simplecli simplesvr upload redirect ssesvr sselci benchmark *.pem

21
example/ssecli.cc Normal file
View file

@ -0,0 +1,21 @@
//
// ssecli.cc
//
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <httplib.h>
#include <iostream>
using namespace std;
int main(void) {
httplib::Client2("http://localhost:1234")
.Get("/event1", [&](const char *data, size_t data_length) {
std::cout << string(data, data_length);
return true;
});
return 0;
}