47 typedef websocketpp::lib::shared_ptr<connection_metadata>
ptr;
52 , m_status(
"Connecting")
61 m_server = con->get_response_header(
"Server");
68 m_server = con->get_response_header(
"Server");
69 m_error_reason = con->get_ec().message();
76 s <<
"close code: " << con->get_remote_close_code() <<
" (" 78 <<
"), close reason: " << con->get_remote_close_reason();
79 m_error_reason = s.str();
84 m_messages.push_back(
"<< " + msg->get_payload());
103 m_messages.push_back(
">> " + message);
110 std::string m_status;
112 std::string m_server;
113 std::string m_error_reason;
114 std::vector<std::string> m_messages;
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";
124 std::vector<std::string>::const_iterator it;
125 for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) {
138 m_endpoint.init_asio();
139 m_endpoint.start_perpetual();
141 m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&
client::run, &m_endpoint);
145 m_endpoint.stop_perpetual();
147 for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
148 if (it->second->get_status() !=
"Open") {
153 std::cout <<
"> Closing connection " << it->second->get_id() << std::endl;
155 websocketpp::lib::error_code ec;
158 std::cout <<
"> Error closing connection " << it->second->get_id() <<
": " 159 << ec.message() << std::endl;
167 websocketpp::lib::error_code ec;
172 std::cout <<
"> Connect initialization error: " << ec.message() << std::endl;
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;
180 con->set_open_handler(websocketpp::lib::bind(
184 websocketpp::lib::placeholders::_1
186 con->set_fail_handler(websocketpp::lib::bind(
190 websocketpp::lib::placeholders::_1
192 con->set_close_handler(websocketpp::lib::bind(
196 websocketpp::lib::placeholders::_1
198 con->set_message_handler(websocketpp::lib::bind(
201 websocketpp::lib::placeholders::_1,
202 websocketpp::lib::placeholders::_2
205 m_endpoint.connect(con);
211 websocketpp::lib::error_code ec;
214 if (metadata_it == m_connection_list.end()) {
215 std::cout <<
"> No connection found with id " <<
id << std::endl;
219 m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec);
221 std::cout <<
"> Error initiating close: " << ec.message() << std::endl;
225 void send(
int id, std::string message) {
226 websocketpp::lib::error_code ec;
229 if (metadata_it == m_connection_list.end()) {
230 std::cout <<
"> No connection found with id " <<
id << std::endl;
236 std::cout <<
"> Error sending message: " << ec.message() << std::endl;
240 metadata_it->second->record_sent_message(message);
244 con_list::const_iterator metadata_it = m_connection_list.find(
id);
245 if (metadata_it == m_connection_list.end()) {
248 return metadata_it->second;
252 typedef std::map<int,connection_metadata::ptr> con_list;
255 websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
257 con_list m_connection_list;
267 std::cout <<
"Enter Command: ";
268 std::getline(std::cin, input);
270 if (input ==
"quit") {
272 }
else if (input ==
"help") {
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" 282 }
else if (input.substr(0,7) ==
"connect") {
283 int id = endpoint.
connect(input.substr(8));
285 std::cout <<
"> Created connection with id " <<
id << std::endl;
287 }
else if (input.substr(0,4) ==
"send") {
288 std::stringstream ss(input);
295 std::getline(ss,message);
297 endpoint.
send(
id, message);
298 }
else if (input.substr(0,5) ==
"close") {
299 std::stringstream ss(input);
306 ss >> cmd >>
id >> close_code;
307 std::getline(ss,reason);
309 endpoint.
close(
id, close_code, reason);
310 }
else if (input.substr(0,4) ==
"show") {
311 int id = atoi(input.substr(5).c_str());
315 std::cout << *metadata << std::endl;
317 std::cout <<
"> Unknown connection id " <<
id << std::endl;
320 std::cout <<
"> Unrecognized Command" << std::endl;
static level const all
Special aggregate value representing "all levels".
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
uint16_t value
The type of a close code value.
connection_metadata::ptr get_metadata(int id) const
websocketpp::client< websocketpp::config::asio_client > client
void close(int id, websocketpp::close::status::value code, std::string reason)
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
static value const normal
Normal closure, meaning that the purpose for which the connection was established has been fulfilled...
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
std::string to_hex(std::string const &input)
Convert std::string to ascii printed string of hex digits.
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
int connect(std::string const &uri)
Table::const_iterator iterator
static value const going_away
The endpoint was "going away", such as a server going down or a browser navigating away from a page...
void send(int id, std::string message)
std::string get_string(value code)
Return a human readable interpretation of a WebSocket close code.
std::size_t run()
wraps the run method of the internal io_service object
static level const all
Special aggregate value representing "all levels".