diff --git a/client_http.hpp b/client_http.hpp index ac5573a..d7f1228 100644 --- a/client_http.hpp +++ b/client_http.hpp @@ -99,12 +99,6 @@ namespace SimpleWeb { }; protected: - class RequestCallback { - public: - bool stop=false; - std::mutex stop_mutex; - }; - class Connection { public: Connection(const std::string &host, unsigned short port, const Config &config, std::unique_ptr &&socket) : diff --git a/http_examples.cpp b/http_examples.cpp index cfd2e58..64ef3b6 100644 --- a/http_examples.cpp +++ b/http_examples.cpp @@ -163,6 +163,10 @@ int main() { } }; + server.on_error=[](std::shared_ptr /*request*/, const SimpleWeb::error_code &/*ec*/) { + // handle errors here + }; + thread server_thread([&server](){ //Start server server.start(); @@ -173,6 +177,8 @@ int main() { //Client examples HttpClient client("localhost:8080"); + + // synchronous request examples auto r1=client.request("GET", "/match/123"); cout << r1->content.rdbuf() << endl; @@ -180,8 +186,13 @@ int main() { auto r2=client.request("POST", "/string", json_string); cout << r2->content.rdbuf() << endl; - auto r3=client.request("POST", "/json", json_string); - cout << r3->content.rdbuf() << endl; + // asynchronous request example + client.request("POST", "/json", json_string, [](std::shared_ptr response, const SimpleWeb::error_code &ec) { + if(!ec) + cout << response->content.rdbuf() << endl; + }); + client.io_service->reset(); // needed because the io_service has been run already in the synchronous examples + client.io_service->run(); server_thread.join(); diff --git a/https_examples.cpp b/https_examples.cpp index ec7a0b7..69d562b 100644 --- a/https_examples.cpp +++ b/https_examples.cpp @@ -159,6 +159,10 @@ int main() { } }; + server.on_error=[](std::shared_ptr /*request*/, const SimpleWeb::error_code &/*ec*/) { + // handle errors here + }; + thread server_thread([&server](){ //Start server server.start(); @@ -170,6 +174,8 @@ int main() { //Client examples //Second Client() parameter set to false: no certificate verification HttpsClient client("localhost:8080", false); + + // synchronous request examples auto r1=client.request("GET", "/match/123"); cout << r1->content.rdbuf() << endl; @@ -177,8 +183,13 @@ int main() { auto r2=client.request("POST", "/string", json_string); cout << r2->content.rdbuf() << endl; - auto r3=client.request("POST", "/json", json_string); - cout << r3->content.rdbuf() << endl; + // asynchronous request example + client.request("POST", "/json", json_string, [](std::shared_ptr response, const SimpleWeb::error_code &ec) { + if(!ec) + cout << response->content.rdbuf() << endl; + }); + client.io_service->reset(); // needed because the io_service has been run already in the synchronous examples + client.io_service->run(); server_thread.join();