【问题标题】:How to establish a secure socket connection with the c++ library websocket++?如何与 c++ 库 websocket++ 建立安全套接字连接?
【发布时间】:2016-02-18 03:35:09
【问题描述】:

我可以在这里使用实用程序客户端建立常规的 WS 连接,https://github.com/zaphoyd/websocketpp/blob/master/tutorials/utility_client/step6.cpp

但是我需要尝试安全的 WS 连接,并用下面的配置替换了配置并链接了所需的库。

typedef websocketpp::client<websocketpp::config::asio_tls_client> client;

我的处理程序看起来像:

typedef std::shared_ptr<boost::asio::ssl::context> context_ptr;

// part of class connection_metadata
static context_ptr on_tls_init(websocketpp::connection_hdl) {
    context_ptr ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);

    try {
        ctx->set_options(boost::asio::ssl::context::default_workarounds |
                         boost::asio::ssl::context::no_sslv2 |
                         boost::asio::ssl::context::no_sslv3 |
                         boost::asio::ssl::context::single_dh_use);
    } catch (std::exception& e) {
        std::cout <<"Error in context pointer: "<< e.what() << std::endl;
    }
    return ctx;
}

// part of class websocket_endpoint 
con->set_tls_init_handler(bind(&connection_metadata::on_tls_init,metadata_ptr,std::placeholders::_1));

当我尝试以下行来获取连接时:

client::connection_ptr con = m_endpoint.get_connection(uri, ec);

我收到:

连接创建尝试失败

【问题讨论】:

    标签: sockets c++11 ssl websocket boost-asio


    【解决方案1】:

    感谢https://groups.google.com/forum/#!topic/websocketpp/SimAUzwZUVM

    我必须将on_tls_init 设为静态函数并像这样使用它(在获取连接之前): m_endpoint.set_tls_init_handler(connection_metadata::on_tls_init)

    其中m_endpoint 是客户端对象。

    【讨论】:

      猜你喜欢
      • 2014-11-20
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      相关资源
      最近更新 更多