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 // **NOTE:** This file is a snapshot of the WebSocket++ utility client tutorial.
28 // Additional related material can be found in the tutorials/utility_client
29 // directory of the WebSocket++ repository.
31 #include <websocketpp/config/asio_no_tls_client.hpp>
32 #include <websocketpp/client.hpp>
34 #include <websocketpp/common/thread.hpp>
35 #include <websocketpp/common/memory.hpp>
43 typedef websocketpp::client<websocketpp::config::asio_client> client;
45 class connection_metadata {
47 typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
49 connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri)
52 , m_status("Connecting")
57 void on_open(client * c, websocketpp::connection_hdl hdl) {
60 client::connection_ptr con = c->get_con_from_hdl(hdl);
61 m_server = con->get_response_header("Server");
64 void on_fail(client * c, websocketpp::connection_hdl hdl) {
67 client::connection_ptr con = c->get_con_from_hdl(hdl);
68 m_server = con->get_response_header("Server");
69 m_error_reason = con->get_ec().message();
72 friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
75 websocketpp::connection_hdl m_hdl;
79 std::string m_error_reason;
82 std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
83 out << "> URI: " << data.m_uri << "\n"
84 << "> Status: " << data.m_status << "\n"
85 << "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
86 << "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason);
91 class websocket_endpoint {
93 websocket_endpoint () : m_next_id(0) {
94 m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
95 m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
97 m_endpoint.init_asio();
98 m_endpoint.start_perpetual();
100 m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&client::run, &m_endpoint);
103 int connect(std::string const & uri) {
104 websocketpp::lib::error_code ec;
106 client::connection_ptr con = m_endpoint.get_connection(uri, ec);
109 std::cout << "> Connect initialization error: " << ec.message() << std::endl;
113 int new_id = m_next_id++;
114 connection_metadata::ptr metadata_ptr = websocketpp::lib::make_shared<connection_metadata>(new_id, con->get_handle(), uri);
115 m_connection_list[new_id] = metadata_ptr;
117 con->set_open_handler(websocketpp::lib::bind(
118 &connection_metadata::on_open,
121 websocketpp::lib::placeholders::_1
123 con->set_fail_handler(websocketpp::lib::bind(
124 &connection_metadata::on_fail,
127 websocketpp::lib::placeholders::_1
130 m_endpoint.connect(con);
135 connection_metadata::ptr get_metadata(int id) const {
136 con_list::const_iterator metadata_it = m_connection_list.find(id);
137 if (metadata_it == m_connection_list.end()) {
138 return connection_metadata::ptr();
140 return metadata_it->second;
144 typedef std::map<int,connection_metadata::ptr> con_list;
147 websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
149 con_list m_connection_list;
156 websocket_endpoint endpoint;
159 std::cout << "Enter Command: ";
160 std::getline(std::cin, input);
162 if (input == "quit") {
164 } else if (input == "help") {
166 << "\nCommand List:\n"
167 << "connect <ws uri>\n"
168 << "show <connection id>\n"
169 << "help: Display this help text\n"
170 << "quit: Exit the program\n"
172 } else if (input.substr(0,7) == "connect") {
173 int id = endpoint.connect(input.substr(8));
175 std::cout << "> Created connection with id " << id << std::endl;
177 } else if (input.substr(0,4) == "show") {
178 int id = atoi(input.substr(5).c_str());
180 connection_metadata::ptr metadata = endpoint.get_metadata(id);
182 std::cout << *metadata << std::endl;
184 std::cout << "> Unknown connection id " << id << std::endl;
187 std::cout << "> Unrecognized Command" << std::endl;
196 clang++ -std=c++11 -stdlib=libc++ -I/Users/zaphoyd/software/websocketpp/ -I/Users/zaphoyd/software/boost_1_55_0/ -D_WEBSOCKETPP_CPP11_STL_ step4.cpp /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_system.a
198 clang++ -I/Users/zaphoyd/software/websocketpp/ -I/Users/zaphoyd/software/boost_1_55_0/ step4.cpp /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_system.a /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_thread.a /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_random.a
200 clang++ -std=c++11 -stdlib=libc++ -I/Users/zaphoyd/Documents/websocketpp/ -I/Users/zaphoyd/Documents/boost_1_53_0_libcpp/ -D_WEBSOCKETPP_CPP11_STL_ step4.cpp /Users/zaphoyd/Documents/boost_1_53_0_libcpp/stage/lib/libboost_system.a