Added heavy work example
This commit is contained in:
parent
3c3359f512
commit
0b397bc697
4 changed files with 24 additions and 1 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 2.8.8)
|
cmake_minimum_required (VERSION 2.8.8)
|
||||||
project (Simple-Web-Server)
|
project (Simple-Web-Server)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -Wall -Wextra")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra")
|
||||||
|
|
||||||
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
|
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
|
||||||
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
|
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,17 @@ int main() {
|
||||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
|
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//Get example simulating heavy work in a separate thread
|
||||||
|
server.resource["^/work$"]["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
|
||||||
|
*response << "HTTP/1.1 200 OK\r\n";
|
||||||
|
thread work_thread([response] {
|
||||||
|
this_thread::sleep_for(chrono::seconds(5));
|
||||||
|
string message="Work done";
|
||||||
|
*response << "Content-Length: " << message.length() << "\r\n\r\n" << message;
|
||||||
|
});
|
||||||
|
work_thread.detach();
|
||||||
|
};
|
||||||
|
|
||||||
//Default GET-example. If no other matches, this anonymous function will be called.
|
//Default GET-example. If no other matches, this anonymous function will be called.
|
||||||
//Will respond with content in the web/-directory, and its subdirectories.
|
//Will respond with content in the web/-directory, and its subdirectories.
|
||||||
//Default file: index.html
|
//Default file: index.html
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,17 @@ int main() {
|
||||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
|
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//Get example simulating heavy work in a separate thread
|
||||||
|
server.resource["^/work$"]["GET"]=[&server](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> /*request*/) {
|
||||||
|
*response << "HTTP/1.1 200 OK\r\n";
|
||||||
|
thread work_thread([response] {
|
||||||
|
this_thread::sleep_for(chrono::seconds(5));
|
||||||
|
string message="Work done";
|
||||||
|
*response << "Content-Length: " << message.length() << "\r\n\r\n" << message;
|
||||||
|
});
|
||||||
|
work_thread.detach();
|
||||||
|
};
|
||||||
|
|
||||||
//Default GET-example. If no other matches, this anonymous function will be called.
|
//Default GET-example. If no other matches, this anonymous function will be called.
|
||||||
//Will respond with content in the web/-directory, and its subdirectories.
|
//Will respond with content in the web/-directory, and its subdirectories.
|
||||||
//Default file: index.html
|
//Default file: index.html
|
||||||
|
|
|
||||||
|
|
@ -181,6 +181,7 @@ namespace SimpleWeb {
|
||||||
io_service.stop();
|
io_service.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///Use this function if you need to recursively send parts of a longer message
|
||||||
void send(std::shared_ptr<Response> response, const std::function<void(const boost::system::error_code&)>& callback=nullptr) const {
|
void send(std::shared_ptr<Response> response, const std::function<void(const boost::system::error_code&)>& callback=nullptr) const {
|
||||||
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
|
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
|
||||||
if(callback)
|
if(callback)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue