Skip to content

Commit

Permalink
SSE client example
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Jul 25, 2020
1 parent 90da199 commit 0e9cfd9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ example/simplecli
example/simplesvr
example/benchmark
example/redirect
example/sse
example/sse*
example/upload
example/*.pem
test/test
Expand Down
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 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 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)
Expand All @@ -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)
Expand All @@ -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 changes: 21 additions & 0 deletions example/ssecli.cc
Original file line number Diff line number Diff line change
@@ -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;
}
File renamed without changes.

0 comments on commit 0e9cfd9

Please sign in to comment.