|
| 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 <iostream> |
| 22 | + |
| 23 | +#include <httpserver.hpp> |
| 24 | + |
| 25 | +#define CHAT_PAGE \ |
| 26 | + "<html>\n" \ |
| 27 | + "<head>\n" \ |
| 28 | + "<title>WebSocket chat</title>\n" \ |
| 29 | + "<script>\n" \ |
| 30 | + "document.addEventListener('DOMContentLoaded', function() {\n" \ |
| 31 | + " const ws = new WebSocket('ws://' + window.location.host + '/ws');\n" \ |
| 32 | + " const btn = document.getElementById('send');\n" \ |
| 33 | + " const msg = document.getElementById('msg');\n" \ |
| 34 | + " const log = document.getElementById('log');\n" \ |
| 35 | + " ws.onopen = function() {\n" \ |
| 36 | + " log.value += 'Connected\\n';\n" \ |
| 37 | + " };\n" \ |
| 38 | + " ws.onclose = function() {\n" \ |
| 39 | + " log.value += 'Disconnected\\n';\n" \ |
| 40 | + " };\n" \ |
| 41 | + " ws.onmessage = function(ev) {\n" \ |
| 42 | + " log.value += ev.data + '\\n';\n" \ |
| 43 | + " };\n" \ |
| 44 | + " btn.onclick = function() {\n" \ |
| 45 | + " log.value += '<You>: ' + msg.value + '\\n';\n" \ |
| 46 | + " ws.send(msg.value);\n" \ |
| 47 | + " };\n" \ |
| 48 | + " msg.onkeyup = function(ev) {\n" \ |
| 49 | + " if (ev.keyCode === 13) {\n" \ |
| 50 | + " ev.preventDefault();\n" \ |
| 51 | + " ev.stopPropagation();\n" \ |
| 52 | + " btn.click();\n" \ |
| 53 | + " msg.value = '';\n" \ |
| 54 | + " }\n" \ |
| 55 | + " };\n" \ |
| 56 | + "});\n" \ |
| 57 | + "</script>\n" \ |
| 58 | + "</head>\n" \ |
| 59 | + "<body>\n" \ |
| 60 | + "<input type='text' id='msg' autofocus/>\n" \ |
| 61 | + "<input type='button' id='send' value='Send' /><br /><br />\n" \ |
| 62 | + "<textarea id='log' rows='20' cols='28'></textarea>\n" \ |
| 63 | + "</body>\n" \ |
| 64 | + "</html>" |
| 65 | + |
| 66 | +class hello_world_resource : public httpserver::http_resource, public httpserver::websocket_handler { |
| 67 | + public: |
| 68 | + const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&); |
| 69 | + virtual std::thread handle_websocket(httpserver::websocket* ws) override; |
| 70 | +}; |
| 71 | + |
| 72 | +// Using the render method you are able to catch each type of request you receive |
| 73 | +const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req) { |
| 74 | + // It is possible to send a response initializing an http_string_response that reads the content to send in response from a string. |
| 75 | + return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(CHAT_PAGE, 200, "text/html")); |
| 76 | +} |
| 77 | + |
| 78 | +std::thread hello_world_resource::handle_websocket(httpserver::websocket* ws) { |
| 79 | + return std::thread([ws]{ |
| 80 | + while (!ws->disconnect()) { |
| 81 | + ws->send("hello world"); |
| 82 | + usleep(1000 * 1000); |
| 83 | + std::string message; |
| 84 | + if (ws->receive(message, 100)) { |
| 85 | + ws->send("server received: " + message); |
| 86 | + } |
| 87 | + } |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +int main() { |
| 92 | + // It is possible to create a webserver passing a great number of parameters. In this case we are just passing the port and the number of thread running. |
| 93 | + httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(1); |
| 94 | + |
| 95 | + hello_world_resource hwr; |
| 96 | + // This way we are registering the hello_world_resource to answer for the endpoint |
| 97 | + // "/hello". The requested method is called (if the request is a GET we call the render_GET |
| 98 | + // method. In case that the specific render method is not implemented, the generic "render" |
| 99 | + // method is called. |
| 100 | + ws.register_resource("/hello", &hwr, true); |
| 101 | + ws.register_resource("/ws", &hwr, true); |
| 102 | + |
| 103 | + // This way we are putting the created webserver in listen. We pass true in order to have |
| 104 | + // a blocking call; if we want the call to be non-blocking we can just pass false to the method. |
| 105 | + ws.start(true); |
| 106 | + return 0; |
| 107 | +} |
0 commit comments