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

Trying to compile a websocketpp endpoint in g++

本文关键字:编译 websocketpp 端点 g++      更新时间:2023-10-16

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

测试.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;
};

测试.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++ 的新手,所以很可能我编译/链接错误或其他什么。

更改:

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

自:

on_message(websocketpp::connection_hdl hdl, message_ptr message)

修复了它。

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