fork download
  1. #include <websocketpp/config/asio_client.hpp>
  2. #include <websocketpp/client.hpp>
  3.  
  4. #include <websocketpp/common/thread.hpp>
  5. #include <websocketpp/common/memory.hpp>
  6.  
  7. #include <cstdlib>
  8. #include <iostream>
  9. #include <map>
  10. #include <string>
  11. #include <sstream>
  12.  
  13. typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
  14. typedef std::shared_ptr<boost::asio::ssl::context> context_ptr;
  15.  
  16. class connection_metadata {
  17. public:
  18. typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
  19.  
  20. connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri)
  21. : m_id(id)
  22. , m_hdl(hdl)
  23. , m_status("Connecting")
  24. , m_uri(uri)
  25. , m_server("N/A")
  26. {}
  27.  
  28. context_ptr on_tls_init(client * c, websocketpp::connection_hdl hdl) {
  29. context_ptr ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
  30. try {
  31. ctx->set_options(boost::asio::ssl::context::default_workarounds |
  32. boost::asio::ssl::context::no_sslv2 |
  33. boost::asio::ssl::context::no_sslv3 |
  34. boost::asio::ssl::context::single_dh_use);
  35. } catch (std::exception &e) {
  36. std::cout << "Error in context pointer: " << e.what() << std::endl;
  37. }
  38. return ctx;
  39. }
  40. void on_open(client * c, websocketpp::connection_hdl hdl) {
  41. m_status = "Open";
  42.  
  43. client::connection_ptr con = c->get_con_from_hdl(hdl);
  44. m_server = con->get_response_header("Server");
  45. }
  46.  
  47. void on_fail(client * c, websocketpp::connection_hdl hdl) {
  48. m_status = "Failed";
  49.  
  50. client::connection_ptr con = c->get_con_from_hdl(hdl);
  51. m_server = con->get_response_header("Server");
  52. m_error_reason = con->get_ec().message();
  53. }
  54.  
  55. void on_close(client * c, websocketpp::connection_hdl hdl) {
  56. m_status = "Closed";
  57. client::connection_ptr con = c->get_con_from_hdl(hdl);
  58. std::stringstream s;
  59. s << "close code: " << con->get_remote_close_code() << " ("
  60. << websocketpp::close::status::get_string(con->get_remote_close_code())
  61. << "), close reason: " << con->get_remote_close_reason();
  62. m_error_reason = s.str();
  63. }
  64.  
  65. void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {
  66. if (msg->get_opcode() == websocketpp::frame::opcode::text) {
  67. m_messages.push_back(msg->get_payload());
  68. } else {
  69. m_messages.push_back(websocketpp::utility::to_hex(msg->get_payload()));
  70. }
  71. }
  72. std::string get_status() const {
  73. return m_status;
  74. }
  75.  
  76. websocketpp::connection_hdl get_hdl() const {
  77. return m_hdl;
  78. }
  79.  
  80. int get_id() const {
  81. return m_id;
  82. }
  83.  
  84. void record_sent_message(std::string message) {
  85. m_messages.push_back(">> " + message);
  86. }
  87.  
  88. friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
  89. private:
  90. int m_id;
  91. websocketpp::connection_hdl m_hdl;
  92. std::string m_status;
  93. std::string m_uri;
  94. std::string m_server;
  95. std::string m_error_reason;
  96. std::vector<std::string> m_messages;
  97. };
  98.  
  99. std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
  100. out << "> URI: " << data.m_uri << "\n"
  101. << "> Status: " << data.m_status << "\n"
  102. << "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
  103. << "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason);
  104. out << "> Messages Processed: (" << data.m_messages.size() << ") \n";
  105.  
  106. std::vector<std::string>::const_iterator it;
  107. for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) {
  108. out << *it << "\n";
  109. }
  110. return out;
  111. }
  112.  
  113. class websocket_endpoint {
  114. public:
  115. websocket_endpoint () : m_next_id(0) {
  116. m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
  117. m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
  118.  
  119. m_endpoint.init_asio();
  120. m_endpoint.start_perpetual();
  121.  
  122. m_thread.reset(new websocketpp::lib::thread(&client::run, &m_endpoint));
  123. }
  124.  
  125. ~websocket_endpoint() {
  126. m_endpoint.stop_perpetual();
  127.  
  128. for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
  129. if (it->second->get_status() != "Open") {
  130. // Only close open connections
  131. continue;
  132. }
  133.  
  134. std::cout << "> Closing connection " << it->second->get_id() << std::endl;
  135.  
  136. websocketpp::lib::error_code ec;
  137. m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec);
  138. if (ec) {
  139. std::cout << "> Error closing connection " << it->second->get_id() << ": "
  140. << ec.message() << std::endl;
  141. }
  142. }
  143.  
  144. m_thread->join();
  145. }
  146.  
  147. int connect(std::string const & uri) {
  148. websocketpp::lib::error_code ec;
  149.  
  150. client::connection_ptr con = m_endpoint.get_connection(uri, ec);
  151. if (ec) {
  152. std::cout << "> Connect initialization error: [" << ec.value() << "] " << ec.message() << std::endl;
  153. return -1;
  154. }
  155.  
  156. int new_id = m_next_id++;
  157. connection_metadata::ptr metadata_ptr(new connection_metadata(new_id, con->get_handle(), uri));
  158. m_connection_list[new_id] = metadata_ptr;
  159.  
  160. con->set_tls_init_handler(websocketpp::lib::bind(
  161. &connection_metadata::on_tls_init,
  162. metadata_ptr,
  163. &m_endpoint,
  164. websocketpp::lib::placeholders::_1
  165. ));
  166. con->set_open_handler(websocketpp::lib::bind(
  167. &connection_metadata::on_open,
  168. metadata_ptr,
  169. &m_endpoint,
  170. websocketpp::lib::placeholders::_1
  171. ));
  172. con->set_fail_handler(websocketpp::lib::bind(
  173. &connection_metadata::on_fail,
  174. metadata_ptr,
  175. &m_endpoint,
  176. websocketpp::lib::placeholders::_1
  177. ));
  178. con->set_close_handler(websocketpp::lib::bind(
  179. &connection_metadata::on_close,
  180. metadata_ptr,
  181. &m_endpoint,
  182. websocketpp::lib::placeholders::_1
  183. ));
  184. con->set_message_handler(websocketpp::lib::bind(
  185. &connection_metadata::on_message,
  186. metadata_ptr,
  187. websocketpp::lib::placeholders::_1,
  188. websocketpp::lib::placeholders::_2
  189. ));
  190. m_endpoint.connect(con);
  191.  
  192. return new_id;
  193. }
  194.  
  195. void close(int id, websocketpp::close::status::value code, std::string reason) {
  196. websocketpp::lib::error_code ec;
  197.  
  198. con_list::iterator metadata_it = m_connection_list.find(id);
  199. if (metadata_it == m_connection_list.end()) {
  200. std::cout << "> No connection found with id " << id << std::endl;
  201. return;
  202. }
  203.  
  204. m_endpoint.close(metadata_it->second->get_hdl(), code, "", ec);
  205. if (ec) {
  206. std::cout << "> Error initiating close: " << ec.message() << std::endl;
  207. }
  208. }
  209.  
  210. void send(int id, std::string message) {
  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.send(metadata_it->second->get_hdl(), message, websocketpp::frame::opcode::text, ec);
  220. if (ec) {
  221. std::cout << "> Error sending message: " << ec.message() << std::endl;
  222. return;
  223. }
  224.  
  225. metadata_it->second->record_sent_message(message);
  226. }
  227.  
  228. connection_metadata::ptr get_metadata(int id) const {
  229. con_list::const_iterator metadata_it = m_connection_list.find(id);
  230. if (metadata_it == m_connection_list.end()) {
  231. return connection_metadata::ptr();
  232. } else {
  233. return metadata_it->second;
  234. }
  235. }
  236.  
  237. private:
  238. typedef std::map<int,connection_metadata::ptr> con_list;
  239.  
  240. client m_endpoint;
  241. websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
  242.  
  243. con_list m_connection_list;
  244. int m_next_id;
  245. };
  246.  
  247. int main() {
  248. bool done = false;
  249. std::string input;
  250. websocket_endpoint endpoint;
  251.  
  252. while (!done) {
  253. std::cout << "Enter Command: ";
  254. std::getline(std::cin, input);
  255.  
  256. if (input == "quit") {
  257. done = true;
  258. } else if (input == "help") {
  259. std::cout
  260. << "\nCommand List:\n"
  261. << "connect <ws uri>\n"
  262. << "show <connection id>\n"
  263. << "help: Display this help text\n"
  264. << "quit: Exit the program\n"
  265. << std::endl;
  266. } else if (input.substr(0,7) == "connect") {
  267. int id = endpoint.connect(input.substr(8));
  268. if (id != -1) {
  269. std::cout << "> Created connection with id " << id << std::endl;
  270. }
  271. } else if (input.substr(0,4) == "show") {
  272. int id = atoi(input.substr(5).c_str());
  273.  
  274. connection_metadata::ptr metadata = endpoint.get_metadata(id);
  275. if (metadata) {
  276. std::cout << *metadata << std::endl;
  277. } else {
  278. std::cout << "> Unknown connection id " << id << std::endl;
  279. }
  280. } else if (input.substr(0,4) == "send") {
  281. std::stringstream ss(input);
  282.  
  283. std::string cmd;
  284. int id;
  285. std::string message = "";
  286.  
  287. ss >> cmd >> id;
  288. std::getline(ss,message);
  289.  
  290. endpoint.send(id, message);
  291. } else if (input.substr(0,5) == "close") {
  292. std::stringstream ss(input);
  293.  
  294. std::string cmd;
  295. int id;
  296. int close_code = websocketpp::close::status::normal;
  297. std::string reason;
  298.  
  299. ss >> cmd >> id >> close_code;
  300. std::getline(ss,reason);
  301.  
  302. endpoint.close(id, close_code, reason);
  303. } else {
  304. std::cout << "> Unrecognized Command" << std::endl;
  305. }
  306. }
  307.  
  308. return 0;
  309. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:10: fatal error: websocketpp/config/asio_client.hpp: No such file or directory
 #include <websocketpp/config/asio_client.hpp>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty