0
でwebsocketppエンドポイントをコンパイルしようとすると:私はファイル++グラムの下で、次のコンパイルしようとしている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++をコンパイルするのは初めてだから、間違っているか何かをコンパイル/リンクしている可能性が高いです。
on_message(websocketpp::connection_hdl &hdl, message_ptr message)
をする:変更
理由は、Visual Studioの長年にわたるバグ/「コンパイラ拡張」です:http://stackoverflow.com/questions/16380966/non-const-reference-bound-to-temporary-visual-studio-bug –
情報ありがとうございます。 – Jack