Skip to content

Commit 0e9cfd9

Browse files
committed
SSE client example
1 parent 90da199 commit 0e9cfd9

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ example/simplecli
77
example/simplesvr
88
example/benchmark
99
example/redirect
10-
example/sse
10+
example/sse*
1111
example/upload
1212
example/*.pem
1313
test/test

README.md

+13-15
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ svr.set_payload_max_length(1024 * 1024 * 512); // 512MB
243243

244244
### Server-Sent Events
245245

246-
Please check [here](https://github.com/yhirose/cpp-httplib/blob/master/example/sse.cc).
246+
[Server example](https://github.com/yhirose/cpp-httplib/blob/master/example/ssesvr.cc)
247+
248+
[Client example](https://github.com/yhirose/cpp-httplib/blob/master/example/ssecli.cc)
247249

248250
### Default thread pool support
249251

@@ -306,20 +308,6 @@ httplib::Headers headers = {
306308
auto res = cli.Get("/hi", headers);
307309
```
308310

309-
### GET with Content Receiver
310-
311-
```c++
312-
std::string body;
313-
314-
auto res = cli.Get("/large-data",
315-
[&](const char *data, size_t data_length) {
316-
body.append(data, data_length);
317-
return true;
318-
});
319-
320-
assert(res->body.empty());
321-
```
322-
323311
### POST
324312

325313
```c++
@@ -390,6 +378,16 @@ cli.set_write_timeout(5, 0); // 5 seconds
390378

391379
### Receive content with Content receiver
392380

381+
```c++
382+
std::string body;
383+
384+
auto res = cli.Get("/large-data",
385+
[&](const char *data, size_t data_length) {
386+
body.append(data, data_length);
387+
return true;
388+
});
389+
```
390+
393391
```cpp
394392
std::string body;
395393
auto res = cli.Get(

example/Makefile

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ OPENSSL_DIR = /usr/local/opt/openssl
55
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
66
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
77

8-
all: server client hello simplecli simplesvr upload redirect sse benchmark
8+
all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark
99

1010
server : server.cc ../httplib.h Makefile
1111
$(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
@@ -28,8 +28,11 @@ upload : upload.cc ../httplib.h Makefile
2828
redirect : redirect.cc ../httplib.h Makefile
2929
$(CXX) -o redirect $(CXXFLAGS) redirect.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
3030

31-
sse : sse.cc ../httplib.h Makefile
32-
$(CXX) -o sse $(CXXFLAGS) sse.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
31+
ssesvr : ssesvr.cc ../httplib.h Makefile
32+
$(CXX) -o ssesvr $(CXXFLAGS) ssesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
33+
34+
ssecli : ssecli.cc ../httplib.h Makefile
35+
$(CXX) -o ssecli $(CXXFLAGS) ssecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
3336

3437
benchmark : benchmark.cc ../httplib.h Makefile
3538
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
@@ -39,4 +42,4 @@ pem:
3942
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
4043

4144
clean:
42-
rm server client hello simplecli simplesvr upload redirect sse benchmark *.pem
45+
rm server client hello simplecli simplesvr upload redirect ssesvr sselci benchmark *.pem

example/ssecli.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// ssecli.cc
3+
//
4+
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
5+
// MIT License
6+
//
7+
8+
#include <httplib.h>
9+
#include <iostream>
10+
11+
using namespace std;
12+
13+
int main(void) {
14+
httplib::Client2("http://localhost:1234")
15+
.Get("/event1", [&](const char *data, size_t data_length) {
16+
std::cout << string(data, data_length);
17+
return true;
18+
});
19+
20+
return 0;
21+
}
File renamed without changes.

0 commit comments

Comments
 (0)