2 * Copyright (c) 2015, 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 #include "tcp_echo_server.hpp"
29 #include <websocketpp/config/asio_no_tls.hpp>
30 #include <websocketpp/server.hpp>
34 using websocketpp::lib::placeholders::_1;
35 using websocketpp::lib::placeholders::_2;
36 using websocketpp::lib::bind;
38 typedef websocketpp::server<websocketpp::config::asio> ws_echo_server;
40 // Define a callback to handle incoming messages
41 void on_message(ws_echo_server* s, websocketpp::connection_hdl hdl, ws_echo_server::message_ptr msg) {
42 std::cout << "on_message called with hdl: " << hdl.lock().get()
43 << " and message: " << msg->get_payload()
46 // check for a special command to instruct the server to stop listening so
47 // it can be cleanly exited.
48 if (msg->get_payload() == "stop-listening") {
54 s->send(hdl, msg->get_payload(), msg->get_opcode());
55 } catch (websocketpp::lib::error_code const & e) {
56 std::cout << "Echo failed because: " << e
57 << "(" << e.message() << ")" << std::endl;
62 asio::io_service service;
64 // Add a TCP echo server on port 9003
65 tcp_echo_server custom_http_server(service, 9003);
67 // Add a WebSocket echo server on port 9002
68 ws_echo_server ws_server;
69 ws_server.set_access_channels(websocketpp::log::alevel::all);
70 ws_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
72 // The only difference in this code between an internal and external
73 // io_service is the different constructor to init_asio
74 ws_server.init_asio(&service);
76 // Register our message handler
77 ws_server.set_message_handler(bind(&on_message,&ws_server,::_1,::_2));
78 ws_server.listen(9002);
79 ws_server.start_accept();
83 // Start the Asio io_service run loop for all