WebSocket++  0.8.0-dev
C++ websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
step6.cpp
1 /*
2  * Copyright (c) 2014, Peter Thorson. All rights reserved.
3  *
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.
14  *
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.
25  */
26 
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.
30 
31 #include <websocketpp/config/asio_no_tls_client.hpp>
32 #include <websocketpp/client.hpp>
33 
34 #include <websocketpp/common/thread.hpp>
35 #include <websocketpp/common/memory.hpp>
36 
37 #include <cstdlib>
38 #include <iostream>
39 #include <map>
40 #include <string>
41 #include <sstream>
42 
43 typedef websocketpp::client<websocketpp::config::asio_client> client;
44 
45 class connection_metadata {
46 public:
47  typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
48 
49  connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri)
50  : m_id(id)
51  , m_hdl(hdl)
52  , m_status("Connecting")
53  , m_uri(uri)
54  , m_server("N/A")
55  {}
56 
57  void on_open(client * c, websocketpp::connection_hdl hdl) {
58  m_status = "Open";
59 
60  client::connection_ptr con = c->get_con_from_hdl(hdl);
61  m_server = con->get_response_header("Server");
62  }
63 
64  void on_fail(client * c, websocketpp::connection_hdl hdl) {
65  m_status = "Failed";
66 
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();
70  }
71 
72  void on_close(client * c, websocketpp::connection_hdl hdl) {
73  m_status = "Closed";
74  client::connection_ptr con = c->get_con_from_hdl(hdl);
75  std::stringstream s;
76  s << "close code: " << con->get_remote_close_code() << " ("
77  << websocketpp::close::status::get_string(con->get_remote_close_code())
78  << "), close reason: " << con->get_remote_close_reason();
79  m_error_reason = s.str();
80  }
81 
82  void on_message(websocketpp::connection_hdl, client::message_ptr msg) {
83  if (msg->get_opcode() == websocketpp::frame::opcode::text) {
84  m_messages.push_back("<< " + msg->get_payload());
85  } else {
86  m_messages.push_back("<< " + websocketpp::utility::to_hex(msg->get_payload()));
87  }
88  }
89 
90  websocketpp::connection_hdl get_hdl() const {
91  return m_hdl;
92  }
93 
94  int get_id() const {
95  return m_id;
96  }
97 
98  std::string get_status() const {
99  return m_status;
100  }
101 
102  void record_sent_message(std::string message) {
103  m_messages.push_back(">> " + message);
104  }
105 
106  friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
107 private:
108  int m_id;
109  websocketpp::connection_hdl m_hdl;
110  std::string m_status;
111  std::string m_uri;
112  std::string m_server;
113  std::string m_error_reason;
114  std::vector<std::string> m_messages;
115 };
116 
117 std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
118  out << "> URI: " << data.m_uri << "\n"
119  << "> Status: " << data.m_status << "\n"
120  << "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
121  << "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason) << "\n";
122  out << "> Messages Processed: (" << data.m_messages.size() << ") \n";
123 
124  std::vector<std::string>::const_iterator it;
125  for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) {
126  out << *it << "\n";
127  }
128 
129  return out;
130 }
131 
132 class websocket_endpoint {
133 public:
134  websocket_endpoint () : m_next_id(0) {
135  m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
136  m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
137 
138  m_endpoint.init_asio();
139  m_endpoint.start_perpetual();
140 
141  m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&client::run, &m_endpoint);
142  }
143 
144  ~websocket_endpoint() {
145  m_endpoint.stop_perpetual();
146 
147  for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
148  if (it->second->get_status() != "Open") {
149  // Only close open connections
150  continue;
151  }
152 
153  std::cout << "> Closing connection " << it->second->get_id() << std::endl;
154 
155  websocketpp::lib::error_code ec;
156  m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec);
157  if (ec) {
158  std::cout << "> Error closing connection " << it->second->get_id() << ": "
159  << ec.message() << std::endl;
160  }
161  }
162 
163  m_thread->join();
164  }
165 
166  int connect(std::string const & uri) {
167  websocketpp::lib::error_code ec;
168 
169  client::connection_ptr con = m_endpoint.get_connection(uri, ec);
170 
171  if (ec) {
172  std::cout << "> Connect initialization error: " << ec.message() << std::endl;
173  return -1;
174  }
175 
176  int new_id = m_next_id++;
177  connection_metadata::ptr metadata_ptr = websocketpp::lib::make_shared<connection_metadata>(new_id, con->get_handle(), uri);
178  m_connection_list[new_id] = metadata_ptr;
179 
180  con->set_open_handler(websocketpp::lib::bind(
181  &connection_metadata::on_open,
182  metadata_ptr,
183  &m_endpoint,
184  websocketpp::lib::placeholders::_1
185  ));
186  con->set_fail_handler(websocketpp::lib::bind(
187  &connection_metadata::on_fail,
188  metadata_ptr,
189  &m_endpoint,
190  websocketpp::lib::placeholders::_1
191  ));
192  con->set_close_handler(websocketpp::lib::bind(
193  &connection_metadata::on_close,
194  metadata_ptr,
195  &m_endpoint,
196  websocketpp::lib::placeholders::_1
197  ));
198  con->set_message_handler(websocketpp::lib::bind(
199  &connection_metadata::on_message,
200  metadata_ptr,
201  websocketpp::lib::placeholders::_1,
202  websocketpp::lib::placeholders::_2
203  ));
204 
205  m_endpoint.connect(con);
206 
207  return new_id;
208  }
209 
210  void close(int id, websocketpp::close::status::value code, std::string reason) {
211  websocketpp::lib::error_code ec;
212 
213  con_list::iterator metadata_it = m_connection_list.find(id);
214  if (metadata_it == m_connection_list.end()) {
215  std::cout << "> No connection found with id " << id << std::endl;
216  return;
217  }
218 
219  m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec);
220  if (ec) {
221  std::cout << "> Error initiating close: " << ec.message() << std::endl;
222  }
223  }
224 
225  void send(int id, std::string message) {
226  websocketpp::lib::error_code ec;
227 
228  con_list::iterator metadata_it = m_connection_list.find(id);
229  if (metadata_it == m_connection_list.end()) {
230  std::cout << "> No connection found with id " << id << std::endl;
231  return;
232  }
233 
234  m_endpoint.send(metadata_it->second->get_hdl(), message, websocketpp::frame::opcode::text, ec);
235  if (ec) {
236  std::cout << "> Error sending message: " << ec.message() << std::endl;
237  return;
238  }
239 
240  metadata_it->second->record_sent_message(message);
241  }
242 
243  connection_metadata::ptr get_metadata(int id) const {
244  con_list::const_iterator metadata_it = m_connection_list.find(id);
245  if (metadata_it == m_connection_list.end()) {
246  return connection_metadata::ptr();
247  } else {
248  return metadata_it->second;
249  }
250  }
251 private:
252  typedef std::map<int,connection_metadata::ptr> con_list;
253 
254  client m_endpoint;
255  websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
256 
257  con_list m_connection_list;
258  int m_next_id;
259 };
260 
261 int main() {
262  bool done = false;
263  std::string input;
264  websocket_endpoint endpoint;
265 
266  while (!done) {
267  std::cout << "Enter Command: ";
268  std::getline(std::cin, input);
269 
270  if (input == "quit") {
271  done = true;
272  } else if (input == "help") {
273  std::cout
274  << "\nCommand List:\n"
275  << "connect <ws uri>\n"
276  << "send <connection id> <message>\n"
277  << "close <connection id> [<close code:default=1000>] [<close reason>]\n"
278  << "show <connection id>\n"
279  << "help: Display this help text\n"
280  << "quit: Exit the program\n"
281  << std::endl;
282  } else if (input.substr(0,7) == "connect") {
283  int id = endpoint.connect(input.substr(8));
284  if (id != -1) {
285  std::cout << "> Created connection with id " << id << std::endl;
286  }
287  } else if (input.substr(0,4) == "send") {
288  std::stringstream ss(input);
289 
290  std::string cmd;
291  int id;
292  std::string message;
293 
294  ss >> cmd >> id;
295  std::getline(ss,message);
296 
297  endpoint.send(id, message);
298  } else if (input.substr(0,5) == "close") {
299  std::stringstream ss(input);
300 
301  std::string cmd;
302  int id;
303  int close_code = websocketpp::close::status::normal;
304  std::string reason;
305 
306  ss >> cmd >> id >> close_code;
307  std::getline(ss,reason);
308 
309  endpoint.close(id, close_code, reason);
310  } else if (input.substr(0,4) == "show") {
311  int id = atoi(input.substr(5).c_str());
312 
313  connection_metadata::ptr metadata = endpoint.get_metadata(id);
314  if (metadata) {
315  std::cout << *metadata << std::endl;
316  } else {
317  std::cout << "> Unknown connection id " << id << std::endl;
318  }
319  } else {
320  std::cout << "> Unrecognized Command" << std::endl;
321  }
322  }
323 
324  return 0;
325 }
326 
327 /*
328 
329 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
330 
331 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
332 
333 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
334 
335 */
friend std::istream & operator>>(std::istream &in, type &t)
Overloaded stream input operator.
Definition: connection.hpp:142