void run_time_limited_client(client & c, std::string uri, long timeout, bool log) { if (log) { c.set_access_channels(websocketpp::log::alevel::all); c.set_error_channels(websocketpp::log::elevel::all); } else { c.clear_access_channels(websocketpp::log::alevel::all); c.clear_error_channels(websocketpp::log::elevel::all); } c.init_asio(); websocketpp::lib::error_code ec; client::connection_ptr con = c.get_connection(uri,ec); BOOST_CHECK( !ec ); c.connect(con); websocketpp::lib::thread tthread(websocketpp::lib::bind( &close_after_timeout<client>, websocketpp::lib::ref(c), con->get_handle(), timeout )); tthread.detach(); c.run(); }
void nextConnection() { static std::string uri; if (connections % CONNECTIONS_PER_INTERFACE == 0) { uri = "ws://127.0.0." + to_string(connections / CONNECTIONS_PER_INTERFACE + 1) + ":" + to_string(port); } websocketpp::lib::error_code ec; connection_ptr con = clientIO.get_connection(uri, ec); clientIO.connect(con); }
int main() { // Remove superfluous logging test_client.clear_access_channels(websocketpp::log::alevel::all); test_client.clear_error_channels(websocketpp::log::elevel::all); // Normal endpoint setup (just as you would with the regular websocketpp::client) test_client.init_asio(); // The endpoint must be perpetual. TODO look at supporting non perpetual (will have to use .reset()) test_client.start_perpetual(); // Start spinning the thread test_thread.reset(new websocketpp::lib::thread(&client::run, &test_client)); // Done boilerplate initialization, now our connection code: websocketpp::lib::error_code ec; client::connection_ptr con = test_client.get_connection("ws://some.fake.server.that.should.not.exist:9001", ec); if(ec) { throw ec; } // Setup any retry_data settings here con->m_retry = true; // Indicates that we do want to attempt to retry connecting (if first attempt fails) con->m_retry_delay = 300; // Will wait 300ms between attempts con->m_max_attempts = 10; // Will stop attempting to retry after 10 attempts try { // Delibrately call connect without setting a configure_handler // This shows the importance that when setting handlers such as // open, message, closed... handlers it must be done within the // configure handler because each retry attempt creates a new // connection using get_connection(...) test_client.connect(con); } catch (const websocketpp::exception & e) { std::cout << e.what() << std::endl; } // Must setup a configure handler, where we register specific connection items (eg. handlers, etc)... con->set_configure_handler(bind(&configure_con, &test_client, ::_1)); std::cout << "Sleeping for 4 seconds to simulate a server that we cannot connect to" << std::endl; std::cout << "and 4 seconds is enough for the 10 retries (@ 300ms delay) to run it's course" << std::endl; // Now connect will start attempting to connect test_client.connect(con); std::this_thread::sleep_for(std::chrono::seconds(6)); test_client.stop_perpetual(); test_thread->join(); return 0; }
void nextHeavyweightConnection() { connected = false; string uri = "ws://127.0.0." + to_string(address) + ":" + to_string(PORT); websocketpp::lib::error_code ec; connection_ptr con = clientIO.get_connection(uri, ec); clientIO.connect(con); while(!connected) { clientIO.run_one(); } updateAddress(); }
void run_client(client & c, std::string uri, bool log = false) { if (log) { c.set_access_channels(websocketpp::log::alevel::all); c.set_error_channels(websocketpp::log::elevel::all); } else { c.clear_access_channels(websocketpp::log::alevel::all); c.clear_error_channels(websocketpp::log::elevel::all); } c.init_asio(); websocketpp::lib::error_code ec; client::connection_ptr con = c.get_connection(uri,ec); BOOST_CHECK( !ec ); c.connect(con); c.run(); }
int main(int argc, char* argv[]) { std::string uri = "ws://localhost:9001"; if (argc == 2) { uri = argv[1]; } try { // We expect there to be a lot of errors, so suppress them sip_client.clear_access_channels(websocketpp::log::alevel::all); sip_client.clear_error_channels(websocketpp::log::elevel::all); // Initialize ASIO sip_client.init_asio(); // Register our handlers sip_client.set_open_handler(bind(&on_open,&sip_client,::_1)); sip_client.set_message_handler(bind(&on_message,&sip_client,::_1,::_2)); websocketpp::lib::error_code ec; client::connection_ptr con = sip_client.get_connection(uri, ec); // Specify the SIP subprotocol: con->add_subprotocol("sip"); sip_client.connect(con); // Start the ASIO io_service run loop sip_client.run(); while(!received) { boost::this_thread::sleep(boost::posix_time::milliseconds(100)); } std::cout << "done" << std::endl; } catch (websocketpp::exception const & e) { std::cout << e.what() << std::endl; } }