Added Server::on_error and async client request examples
This commit is contained in:
parent
1d34b7f3de
commit
62da9daf87
3 changed files with 26 additions and 10 deletions
|
|
@ -99,12 +99,6 @@ namespace SimpleWeb {
|
||||||
};
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
class RequestCallback {
|
|
||||||
public:
|
|
||||||
bool stop=false;
|
|
||||||
std::mutex stop_mutex;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
public:
|
public:
|
||||||
Connection(const std::string &host, unsigned short port, const Config &config, std::unique_ptr<socket_type> &&socket) :
|
Connection(const std::string &host, unsigned short port, const Config &config, std::unique_ptr<socket_type> &&socket) :
|
||||||
|
|
|
||||||
|
|
@ -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](){
|
thread server_thread([&server](){
|
||||||
//Start server
|
//Start server
|
||||||
server.start();
|
server.start();
|
||||||
|
|
@ -173,6 +177,8 @@ int main() {
|
||||||
|
|
||||||
//Client examples
|
//Client examples
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
|
|
||||||
|
// synchronous request examples
|
||||||
auto r1=client.request("GET", "/match/123");
|
auto r1=client.request("GET", "/match/123");
|
||||||
cout << r1->content.rdbuf() << endl;
|
cout << r1->content.rdbuf() << endl;
|
||||||
|
|
||||||
|
|
@ -180,8 +186,13 @@ int main() {
|
||||||
auto r2=client.request("POST", "/string", json_string);
|
auto r2=client.request("POST", "/string", json_string);
|
||||||
cout << r2->content.rdbuf() << endl;
|
cout << r2->content.rdbuf() << endl;
|
||||||
|
|
||||||
auto r3=client.request("POST", "/json", json_string);
|
// asynchronous request example
|
||||||
cout << r3->content.rdbuf() << endl;
|
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();
|
server_thread.join();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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](){
|
thread server_thread([&server](){
|
||||||
//Start server
|
//Start server
|
||||||
server.start();
|
server.start();
|
||||||
|
|
@ -170,6 +174,8 @@ int main() {
|
||||||
//Client examples
|
//Client examples
|
||||||
//Second Client() parameter set to false: no certificate verification
|
//Second Client() parameter set to false: no certificate verification
|
||||||
HttpsClient client("localhost:8080", false);
|
HttpsClient client("localhost:8080", false);
|
||||||
|
|
||||||
|
// synchronous request examples
|
||||||
auto r1=client.request("GET", "/match/123");
|
auto r1=client.request("GET", "/match/123");
|
||||||
cout << r1->content.rdbuf() << endl;
|
cout << r1->content.rdbuf() << endl;
|
||||||
|
|
||||||
|
|
@ -177,8 +183,13 @@ int main() {
|
||||||
auto r2=client.request("POST", "/string", json_string);
|
auto r2=client.request("POST", "/string", json_string);
|
||||||
cout << r2->content.rdbuf() << endl;
|
cout << r2->content.rdbuf() << endl;
|
||||||
|
|
||||||
auto r3=client.request("POST", "/json", json_string);
|
// asynchronous request example
|
||||||
cout << r3->content.rdbuf() << endl;
|
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();
|
server_thread.join();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue