|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <httpserver.hpp> |
| 22 | +#include <iostream> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +using namespace httpserver; |
| 26 | + |
| 27 | +std::string topics_array[] = { "topic_1" }; |
| 28 | +std::vector<std::string> topics(topics_array, topics_array + sizeof(topics_array) / sizeof(std::string)); |
| 29 | + |
| 30 | +class comet_send_resource : public http_resource { |
| 31 | + public: |
| 32 | + void render(const http_request& req, http_response** res) |
| 33 | + { |
| 34 | + *res = new http_response(http_response_builder("Hi", 200).long_polling_send_response(topics_array[0])); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +class comet_listen_resource : public http_resource { |
| 39 | + public: |
| 40 | + void render(const http_request& req, http_response** res) |
| 41 | + { |
| 42 | + *res = new http_response(http_response_builder("OK", 200).long_polling_receive_response(topics)); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +int main() |
| 47 | +{ |
| 48 | + //it is possible to create a webserver passing a great number of parameters. |
| 49 | + //In this case we are just passing the port and the number of thread running. |
| 50 | + webserver ws = create_webserver(8080).start_method(http::http_utils::INTERNAL_SELECT); |
| 51 | + |
| 52 | + comet_send_resource csr; |
| 53 | + comet_listen_resource clr; |
| 54 | + //this way we are registering the hello_world_resource to answer for the endpoint |
| 55 | + //"/hello". The requested method is called (if the request is a GET we call the render_GET |
| 56 | + //method. In case that the specific render method is not implemented, the generic "render" |
| 57 | + //method is called. |
| 58 | + ws.register_resource("/send", &csr, true); |
| 59 | + ws.register_resource("/listen", &clr, true); |
| 60 | + |
| 61 | + //This way we are putting the created webserver in listen. We pass true in order to have |
| 62 | + //a blocking call; if we want the call to be non-blocking we can just pass false to the |
| 63 | + //method. |
| 64 | + ws.start(true); |
| 65 | + return 0; |
| 66 | +} |
0 commit comments