nokome
nokome

Reputation: 10314

Websocketpp simple HTTP client

I use the excellent websocketpp library to provide a Websockets (and HTTP) server in a C++ application. I also need a HTTP client in the same app to connect to REST APIs. I have been attempting this in websocketpp also, but so far I have had little success. The following preliminary attempt gives me this log output:

[2015-03-06 18:01:18] [connect] Successful connection
[2015-03-06 18:01:18] [error] Server handshake response error: websocketpp.processor:20 (Invalid HTTP status.)
[2015-03-06 18:01:18] [disconnect] Failed: Invalid HTTP status.

This suggests my http_ handler method may need something more. Any advice would be appreciated. The websocketpp docs and examples don't seem to include a simple HTTP client.

#define _WEBSOCKETPP_CPP11_STL_
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>

namespace {

using namespace websocketpp;
typedef client<websocketpp::config::asio_client> client;

class Client {
public:

    Client(void){
        client_.init_asio();
        client_.set_http_handler(bind(&Client::http_,this,_1));
    }

    std::string get(const std::string& url) {
        websocketpp::lib::error_code error;
        client::connection_ptr con = client_.get_connection(url,error);
        if(error) std::runtime_error("Unable to connnect.\n  url: "+url+"\n  message: "+error.message());
        client_.connect(con);
        websocketpp::lib::thread asio_thread(&client::run, &client_);
        asio_thread.join();
        return data_;
    }

private:

    void http_(connection_hdl hdl){
        std::cout<<"Connected\n";
        data_ = "http payload";
    }

    client client_;
    std::string data_;
};

}

int main(void){
    Client client;
    client.get("http://google.com/");
}

Upvotes: 3

Views: 9773

Answers (3)

Vinnie Falco
Vinnie Falco

Reputation: 5353

If you're trying to do both WebSocket and HTTP in C++ there's a great library called Beast that has BOTH of these things! Its open source and builds on Boost.Asio: https://github.com/vinniefalco/Beast/

Here's some example code:

Use HTTP to request the root page from a website and print the response:

#include <beast/http.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>

int main()
{
    // Normal boost::asio setup
    std::string const host = "boost.org";
    boost::asio::io_service ios;
    boost::asio::ip::tcp::resolver r(ios);
    boost::asio::ip::tcp::socket sock(ios);
    boost::asio::connect(sock,
        r.resolve(boost::asio::ip::tcp::resolver::query{host, "http"}));

    // Send HTTP request using beast
    beast::http::request_v1<beast::http::empty_body> req;
    req.method = "GET";
    req.url = "/";
    req.version = 11;
    req.headers.replace("Host", host + ":" + std::to_string(sock.remote_endpoint().port()));
    req.headers.replace("User-Agent", "Beast");
    beast::http::prepare(req);
    beast::http::write(sock, req);

    // Receive and print HTTP response using beast
    beast::streambuf sb;
    beast::http::response_v1<beast::http::streambuf_body> resp;
    beast::http::read(sock, sb, resp);
    std::cout << resp;
}

Establish a WebSocket connection, send a message and receive the reply:

#include <beast/to_string.hpp>
#include <beast/websocket.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>

int main()
{
    // Normal boost::asio setup
    std::string const host = "echo.websocket.org";
    boost::asio::io_service ios;
    boost::asio::ip::tcp::resolver r(ios);
    boost::asio::ip::tcp::socket sock(ios);
    boost::asio::connect(sock,
        r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));

    // WebSocket connect and send message using beast
    beast::websocket::stream<boost::asio::ip::tcp::socket&> ws(sock);
    ws.handshake(host, "/");
    ws.write(boost::asio::buffer("Hello, world!"));

    // Receive WebSocket message, print and close using beast
    beast::streambuf sb;
    beast::websocket::opcode op;
    ws.read(op, sb);
    ws.close(beast::websocket::close_code::normal);
    std::cout << to_string(sb.data()) << "\n";
}

Upvotes: 2

zaphoyd
zaphoyd

Reputation: 2760

WebSocket++'s HTTP handling features are a convenience feature designed to allow WebSocket servers to serve HTTP responses in a limited capacity. WebSocket++ is not intended for use as a generic HTTP library and does not contain the ability to play the role of a (non-WebSocket) HTTP client.

Using a separate library (such as cpp-netlib) for HTTP client functionality is a good solution.

Upvotes: 6

nokome
nokome

Reputation: 10314

I did not know how to prevent the websocketpp client from asking for a Upgrade: connection so I ended up using cpp-netlib for a HTTP client instead.

Upvotes: -2

Related Questions