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(); }
int main(int argc, char **argv) { clientIO.clear_access_channels(websocketpp::log::alevel::all); clientIO.init_asio(); clientIO.set_message_handler([](websocketpp::connection_hdl hdl, client::message_ptr msg) { if (ECHO_MESSAGE != msg->get_payload()) { cout << "Error echo" << endl; exit(-1); } echoed = true; }); clientIO.set_open_handler([&clientIO](websocketpp::connection_hdl hdl) { connected = true; heavyConnections.push_back(hdl); }); startPoint = chrono::system_clock::now(); while(connections < TOTAL_CONNECTIONS) { // Idle connections are made very lightweight for (int i = 0; i < IDLE_CONNECTIONS_PER_ACTIVE_CONNECTION && connections < TOTAL_CONNECTIONS; i++) { if (nextLightweightConnection()) { return -1; } } // Active connections are "real" using WebSocket++ nextHeavyweightConnection(); } cout << "----------------------------------" << endl; cout << "Active connections: " << heavyConnections.size() << endl; cout << "Idle connections: " << (connections - heavyConnections.size()) << endl; float connectionsPerSecond = float(connections) / chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now() - startPoint).count(); cout << "Connection performance: " << connectionsPerSecond << " connections/ms" << endl; // start sending messages using the active connections cout << "----------------------------------" << endl; startPoint = chrono::system_clock::now(); for (int i = 0; i < heavyConnections.size(); i++) { echoMessage(i); } cout << "Echo delay, microseconds: " << chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now() - startPoint).count() << endl; cout << "ALL DONE" << endl; clientIO.run(); return 0; }
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 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; } }
int main(int argc, char **argv) { if (argc != 3) { cout << "Usage: bench2 numberOfConnections port" << endl; } totalConnections = atoi(argv[1]); port = atoi(argv[2]); clientIO.clear_access_channels(websocketpp::log::alevel::all); clientIO.init_asio(); clientIO.set_message_handler([](websocketpp::connection_hdl hdl, client::message_ptr msg) { if (ECHO_MESSAGE != msg->get_payload()) { cout << "Error: echo not verbatim!" << endl; exit(-1); } if (++received == sent) { cout << "Echo performance: " << double(sent) / (1e-3 * duration_cast<microseconds>(high_resolution_clock::now() - startPoint).count()) << " echoes/ms" << endl; echo(connections, MESSAGES_PER_CONNECTION); } }); clientIO.set_open_handler([](websocketpp::connection_hdl hdl) { connectionVector.push_back(hdl); if (++connections < totalConnections) { nextConnection(); } else { startPoint = high_resolution_clock::now(); echo(connections, MESSAGES_PER_CONNECTION); } }); nextConnection(); clientIO.run(); return 0; }