2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the WebSocket++ Project nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /** ====== WARNING ========
28 * This example is presently used as a scratch space. It may or may not be broken
32 #include <websocketpp/config/debug_asio_no_tls.hpp>
35 #include <websocketpp/logger/syslog.hpp>
37 #include <websocketpp/server.hpp>
41 ////////////////////////////////////////////////////////////////////////////////
42 ///////////////// Custom Config for debugging custom policies //////////////////
43 ////////////////////////////////////////////////////////////////////////////////
45 struct debug_custom : public websocketpp::config::debug_asio {
46 typedef debug_custom type;
47 typedef debug_asio base;
49 typedef base::concurrency_type concurrency_type;
51 typedef base::request_type request_type;
52 typedef base::response_type response_type;
54 typedef base::message_type message_type;
55 typedef base::con_msg_manager_type con_msg_manager_type;
56 typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
58 /// Custom Logging policies
59 /*typedef websocketpp::log::syslog<concurrency_type,
60 websocketpp::log::elevel> elog_type;
61 typedef websocketpp::log::syslog<concurrency_type,
62 websocketpp::log::alevel> alog_type;
64 typedef base::alog_type alog_type;
65 typedef base::elog_type elog_type;
67 typedef base::rng_type rng_type;
69 struct transport_config : public base::transport_config {
70 typedef type::concurrency_type concurrency_type;
71 typedef type::alog_type alog_type;
72 typedef type::elog_type elog_type;
73 typedef type::request_type request_type;
74 typedef type::response_type response_type;
75 typedef websocketpp::transport::asio::basic_socket::endpoint
79 typedef websocketpp::transport::asio::endpoint<transport_config>
82 static const long timeout_open_handshake = 0;
85 ////////////////////////////////////////////////////////////////////////////////
87 typedef websocketpp::server<debug_custom> server;
89 using websocketpp::lib::placeholders::_1;
90 using websocketpp::lib::placeholders::_2;
91 using websocketpp::lib::bind;
93 // pull out the type of messages sent by our config
94 typedef server::message_ptr message_ptr;
96 bool validate(server *, websocketpp::connection_hdl) {
101 void on_http(server* s, websocketpp::connection_hdl hdl) {
102 server::connection_ptr con = s->get_con_from_hdl(hdl);
104 std::string res = con->get_request_body();
106 std::stringstream ss;
107 ss << "got HTTP request with " << res.size() << " bytes of body data.";
109 con->set_body(ss.str());
110 con->set_status(websocketpp::http::status_code::ok);
113 void on_fail(server* s, websocketpp::connection_hdl hdl) {
114 server::connection_ptr con = s->get_con_from_hdl(hdl);
116 std::cout << "Fail handler: " << con->get_ec() << " " << con->get_ec().message() << std::endl;
119 void on_close(websocketpp::connection_hdl) {
120 std::cout << "Close handler" << std::endl;
123 // Define a callback to handle incoming messages
124 void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
125 std::cout << "on_message called with hdl: " << hdl.lock().get()
126 << " and message: " << msg->get_payload()
130 s->send(hdl, msg->get_payload(), msg->get_opcode());
131 } catch (const websocketpp::lib::error_code& e) {
132 std::cout << "Echo failed because: " << e
133 << "(" << e.message() << ")" << std::endl;
138 // Create a server endpoint
142 // Set logging settings
143 echo_server.set_access_channels(websocketpp::log::alevel::all);
144 echo_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
147 echo_server.init_asio();
148 echo_server.set_reuse_addr(true);
150 // Register our message handler
151 echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
153 echo_server.set_http_handler(bind(&on_http,&echo_server,::_1));
154 echo_server.set_fail_handler(bind(&on_fail,&echo_server,::_1));
155 echo_server.set_close_handler(&on_close);
157 echo_server.set_validate_handler(bind(&validate,&echo_server,::_1));
159 // Listen on port 9012
160 echo_server.listen(9012);
162 // Start the server accept loop
163 echo_server.start_accept();
165 // Start the ASIO io_service run loop
167 } catch (const std::exception & e) {
168 std::cout << e.what() << std::endl;
169 } catch (websocketpp::lib::error_code e) {
170 std::cout << e.message() << std::endl;
172 std::cout << "other exception" << std::endl;