Beispiel #1
0
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();
}
Beispiel #2
0
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;
}
Beispiel #3
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();
}
Beispiel #4
0
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;
    }
}
Beispiel #5
0
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;
}