尝试在 g++ 中编译 websocketpp 端点

Posted

技术标签:

【中文标题】尝试在 g++ 中编译 websocketpp 端点【英文标题】:Trying to compile a websocketpp endpoint in g++ 【发布时间】:2016-07-11 13:32:34 【问题描述】:

我正在尝试在g++下编译以下文件:

Test.hpp

#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_client.hpp>

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

using websocketpp::lib::bind;

typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
typedef client::connection_ptr connection_ptr;

class ClientConnection 
public:
    ClientConnection();

    void start(std::string uri);

    // Event handlers
    context_ptr on_tls_init(websocketpp::connection_hdl);
    void on_fail(websocketpp::connection_hdl hdl);
    void on_open(websocketpp::connection_hdl hdl);
    void on_message(websocketpp::connection_hdl &hdl, message_ptr message);
    void on_close(websocketpp::connection_hdl);

private:
    client c;
;

Test.cpp:

#include <iostream>

#include "Test.hpp"

ClientConnection::ClientConnection() 
    // Reset the log channels
    c.clear_access_channels(websocketpp::log::alevel::all);

    c.set_access_channels(websocketpp::log::alevel::app | websocketpp::log::alevel::connect);
    c.set_error_channels(websocketpp::log::elevel::all);

    // Initialize ASIO
    c.init_asio();

    // Bind handlers
    c.set_tls_init_handler(bind<context_ptr>(
        &ClientConnection::on_tls_init,
        this,
        websocketpp::lib::placeholders::_1
    ));
    c.set_message_handler(bind(
        &ClientConnection::on_message,
        this,
        websocketpp::lib::placeholders::_1,
        websocketpp::lib::placeholders::_2
    ));
    c.set_open_handler(bind(
        &ClientConnection::on_open,
        this,
        websocketpp::lib::placeholders::_1
    ));
    c.set_close_handler(bind(
        &ClientConnection::on_close,
        this,
        websocketpp::lib::placeholders::_1
    ));
    c.set_fail_handler(bind(
        &ClientConnection::on_fail,
        this,
        websocketpp::lib::placeholders::_1
    ));


// Open a connection to the URI provided
void ClientConnection::start(std::string uri) 
    websocketpp::lib::error_code ec;
    client::connection_ptr con = c.get_connection(uri, ec);

    if (ec)  // failed to create connection
        c.get_alog().write(websocketpp::log::alevel::app, ec.message());
        return;
    

    // Open the connection
    c.connect(con);
    c.run();


context_ptr ClientConnection::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;


void ClientConnection::on_fail(websocketpp::connection_hdl hdl) 
    client::connection_ptr con = c.get_con_from_hdl(hdl);

    // Print as much information as possible
    c.get_elog().write(websocketpp::log::elevel::warn,
        "Fail handler: \n" +
        std::to_string(con->get_state()) + "\n" +
        std::to_string(con->get_local_close_code()) + "\n" +
        con->get_local_close_reason() + "\n" +
        std::to_string(con->get_remote_close_code()) + "\n" +
        con->get_remote_close_reason() + "\n" +
        std::to_string(con->get_ec().value()) + " - " + con->get_ec().message() + "\n");


void ClientConnection::on_open(websocketpp::connection_hdl hdl) 


void ClientConnection::on_message(websocketpp::connection_hdl &hdl, message_ptr message) 
    std::cout << "<< " << message->get_payload() << std::endl;


void ClientConnection::on_close(websocketpp::connection_hdl) 
    std::cout << "Closed." << std::endl;

但是,在尝试编译时会抛出以下错误(包括 g++ 编译命令):

> g++ Test.cpp -o Test.o -I./ -I../../lib/websocketpp -I~/boost_1_61_0 -L/usr/local/lib -I/usr/local/include -lpthread -lboost_system -lboost_random -lboost_timer -lboost_chrono -lrt -lssl -lcrypto -std=c++14

Test.cpp: In constructor ‘ClientConnection::ClientConnection()’:
Test.cpp:26:3: error: no matching function for call to ‘websocketpp::client<websocketpp::config::asio_tls_client>::set_message_handler(std::_Bind_helper<false, void (ClientConnection::*)(std::weak_ptr<void>&, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >), ClientConnection*, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type)’
  ));
   ^
Test.cpp:26:3: note: candidate is:
In file included from ../../lib/websocketpp/websocketpp/roles/client_endpoint.hpp:31:0,
                 from ../../lib/websocketpp/websocketpp/client.hpp:31,
                 from Test.hpp:1,
                 from Test.cpp:3:
../../lib/websocketpp/websocketpp/endpoint.hpp:322:10: note: void websocketpp::endpoint<connection, config>::set_message_handler(websocketpp::endpoint<connection, config>::message_handler) [with connection = websocketpp::connection<websocketpp::config::asio_tls_client>; config = websocketpp::config::asio_tls_client; websocketpp::endpoint<connection, config>::message_handler = std::function<void(std::weak_ptr<void>, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >)>]
     void set_message_handler(message_handler h) 
          ^
../../lib/websocketpp/websocketpp/endpoint.hpp:322:10: note:   no known conversion for argument 1 from ‘std::_Bind_helper<false, void (ClientConnection::*)(std::weak_ptr<void>&, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >), ClientConnection*, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type aka std::_Bind<std::_Mem_fn<void (ClientConnection::*)(std::weak_ptr<void>&, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >)>(ClientConnection*, std::_Placeholder<1>, std::_Placeholder<2>)>’ to ‘websocketpp::endpoint<websocketpp::connection<websocketpp::config::asio_tls_client>, websocketpp::config::asio_tls_client>::message_handler aka std::function<void(std::weak_ptr<void>, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >)>’

我确定我做错了什么,但不确定是什么。任何帮助表示赞赏!我是在 Linux 下编译 c++ 的新手,所以很可能我在编译/链接错误之类的。

【问题讨论】:

【参考方案1】:

变化:

on_message(websocketpp::connection_hdl &hdl, message_ptr message)

到:

on_message(websocketpp::connection_hdl hdl, message_ptr message)

修复它。

但是,前面的代码在 Visual Studio C++ 下编译。有谁知道为什么会这样?

【讨论】:

原因是,Visual Studio 中长期存在的错误/“编译器扩展”:***.com/questions/16380966/… 感谢您的信息。

以上是关于尝试在 g++ 中编译 websocketpp 端点的主要内容,如果未能解决你的问题,请参考以下文章

websocketpp库中的错误和Windows Visual Studio 2015中的提升[关闭]

websocketpp 最简单的客户端 一

WebRTC笔记之十八:WebSocketPP

WebRTC笔记之十八:WebSocketPP

在 Windows 上使用 websocketpp 和 MySQL 提升构建错误

websocket和websocket++和websocketpp都是啥,有啥关系呢