Added Server::on_error and async client request examples

This commit is contained in:
eidheim 2017-06-20 14:36:05 +02:00
commit 62da9daf87
3 changed files with 26 additions and 10 deletions

View file

@ -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_type> &&socket) :

View file

@ -163,6 +163,10 @@ int main() {
}
};
server.on_error=[](std::shared_ptr<HttpServer::Request> /*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<HttpClient::Response> 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();

View file

@ -159,6 +159,10 @@ int main() {
}
};
server.on_error=[](std::shared_ptr<HttpsServer::Request> /*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<HttpsClient::Response> 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();