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();
100 std::string m_server;
101 std::string m_error_reason;
105 out <<
"> URI: " << data.m_uri <<
"\n" 106 <<
"> Status: " << data.m_status <<
"\n" 107 <<
"> Remote Server: " << (data.m_server.empty() ?
"None Specified" : data.m_server) <<
"\n" 108 <<
"> Error/close reason: " << (data.m_error_reason.empty() ?
"N/A" : data.m_error_reason);
119 m_endpoint.init_asio();
120 m_endpoint.start_perpetual();
122 m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&
client::run, &m_endpoint);
126 m_endpoint.stop_perpetual();
128 for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
129 if (it->second->get_status() !=
"Open") {
134 std::cout <<
"> Closing connection " << it->second->get_id() << std::endl;
136 websocketpp::lib::error_code ec;
139 std::cout <<
"> Error closing connection " << it->second->get_id() <<
": " 140 << ec.message() << std::endl;
148 websocketpp::lib::error_code ec;
153 std::cout <<
"> Connect initialization error: " << ec.message() << std::endl;
157 int new_id = m_next_id++;
158 connection_metadata::ptr metadata_ptr = websocketpp::lib::make_shared<connection_metadata>(new_id, con->get_handle(), uri);
159 m_connection_list[new_id] = metadata_ptr;
161 con->set_open_handler(websocketpp::lib::bind(
165 websocketpp::lib::placeholders::_1
167 con->set_fail_handler(websocketpp::lib::bind(
171 websocketpp::lib::placeholders::_1
173 con->set_close_handler(websocketpp::lib::bind(
177 websocketpp::lib::placeholders::_1
180 m_endpoint.connect(con);
186 websocketpp::lib::error_code ec;
189 if (metadata_it == m_connection_list.end()) {
190 std::cout <<
"> No connection found with id " <<
id << std::endl;
194 m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec);
196 std::cout <<
"> Error initiating close: " << ec.message() << std::endl;
201 con_list::const_iterator metadata_it = m_connection_list.find(
id);
202 if (metadata_it == m_connection_list.end()) {
205 return metadata_it->second;
209 typedef std::map<int,connection_metadata::ptr> con_list;
212 websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
214 con_list m_connection_list;
224 std::cout <<
"Enter Command: ";
225 std::getline(std::cin, input);
227 if (input ==
"quit") {
229 }
else if (input ==
"help") {
231 <<
"\nCommand List:\n" 232 <<
"connect <ws uri>\n" 233 <<
"close <connection id> [<close code:default=1000>] [<close reason>]\n" 234 <<
"show <connection id>\n" 235 <<
"help: Display this help text\n" 236 <<
"quit: Exit the program\n" 238 }
else if (input.substr(0,7) ==
"connect") {
239 int id = endpoint.
connect(input.substr(8));
241 std::cout <<
"> Created connection with id " <<
id << std::endl;
243 }
else if (input.substr(0,5) ==
"close") {
244 std::stringstream ss(input);
251 ss >> cmd >>
id >> close_code;
252 std::getline(ss,reason);
254 endpoint.
close(
id, close_code, reason);
255 }
else if (input.substr(0,4) ==
"show") {
256 int id = atoi(input.substr(5).c_str());
260 std::cout << *metadata << std::endl;
262 std::cout <<
"> Unknown connection id " <<
id << std::endl;
265 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
void close(int id, websocketpp::close::status::value code, std::string reason)
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.
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...
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".
websocketpp::client< websocketpp::config::asio_client > client