Websocket++ error: handle_transport_init received error: TLS handshake failed
Websocket++ error: handle_transport_init received error: TLS handshake failed
当我连接到本地主机时,它起作用了。但是当我尝试连接到 wss://echo.websocket.org 它说:
[2020-07-03 00:24:19] [connect] 连接成功
[2020-07-03 00:24:20] [错误] handle_transport_init 收到错误:TLS 握手失败
[2020-07-03 00:24:20] [失败] WebSocket 连接 174.129.224.73:443 - "" / 0
websocketpp.transport.asio.socket:8 TLS 握手失败
[2020-07-03 00:24:20] [info] asio async_shutdown 错误:asio.ssl:336462231(在初始化时关机)
我需要做什么才能让它发挥作用?
部分代码:
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
struct connection_data
{
bool status;
string error;
vector <string> messages;
client::connection_ptr con;
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
};
struct connection_data m_con;
client cl;
using namespace std;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg)
{
m_con.messages.push_back(msg->get_payload());
}
void on_open(client* c, websocketpp::connection_hdl hdl)
{
m_con.status = true;
}
void on_close(client* c, websocketpp::connection_hdl hdl)
{
m_con.status = false;
m_con.con = c->get_con_from_hdl(hdl);
m_con.messages.clear();
m_con.error.clear();
}
context_ptr on_tls_init(const char* hostname, websocketpp::connection_hdl) {
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv13);
return ctx;
}
void on_http(client* c, websocketpp::http_handler hdl)
{
}
void run()
{
cout << "Thread enter" << endl;
cl.run();
}
DLL void websock_open(char* address)
{
string uri = string(address);
websocketpp::lib::error_code ec;
try {
cl.set_access_channels(websocketpp::log::alevel::all);
cl.clear_access_channels(websocketpp::log::alevel::all);
cl.init_asio();
// Setting callbacks
cl.set_message_handler(bind(&on_message, &cl, ::_1, ::_2));
cl.set_open_handler(bind(&on_open, &cl, ::_1));
cl.set_close_handler(bind(&on_close, &cl, ::_1));
cl.set_tls_init_handler(bind(&on_tls_init, string(address).c_str(), ::_1));
m_con.con = cl.get_connection(uri, ec);
if (ec) {
m_con.error = "Can't connect: " + ec.message();
}
cl.connect(m_con.con);
cout << "connected" << endl;
m_con.m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&run);
cout << "Thread created" << endl;
m_con.status = true;
} catch (websocketpp::exception const & e) {
cout << e.what() << endl;
}
}
我解决了这个问题。验证证书时出错。
修复它的代码:
context_ptr on_tls_init(const char* hostname, websocketpp::connection_hdl) {
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv12);
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);
ctx->set_verify_mode(boost::asio::ssl::verify_none);
} catch (std::exception& e) {
m_con.error = string(e.what());
}
return ctx;
}
当我连接到本地主机时,它起作用了。但是当我尝试连接到 wss://echo.websocket.org 它说:
[2020-07-03 00:24:19] [connect] 连接成功
[2020-07-03 00:24:20] [错误] handle_transport_init 收到错误:TLS 握手失败
[2020-07-03 00:24:20] [失败] WebSocket 连接 174.129.224.73:443 - "" / 0 websocketpp.transport.asio.socket:8 TLS 握手失败
[2020-07-03 00:24:20] [info] asio async_shutdown 错误:asio.ssl:336462231(在初始化时关机)
我需要做什么才能让它发挥作用?
部分代码:
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
struct connection_data
{
bool status;
string error;
vector <string> messages;
client::connection_ptr con;
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
};
struct connection_data m_con;
client cl;
using namespace std;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg)
{
m_con.messages.push_back(msg->get_payload());
}
void on_open(client* c, websocketpp::connection_hdl hdl)
{
m_con.status = true;
}
void on_close(client* c, websocketpp::connection_hdl hdl)
{
m_con.status = false;
m_con.con = c->get_con_from_hdl(hdl);
m_con.messages.clear();
m_con.error.clear();
}
context_ptr on_tls_init(const char* hostname, websocketpp::connection_hdl) {
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv13);
return ctx;
}
void on_http(client* c, websocketpp::http_handler hdl)
{
}
void run()
{
cout << "Thread enter" << endl;
cl.run();
}
DLL void websock_open(char* address)
{
string uri = string(address);
websocketpp::lib::error_code ec;
try {
cl.set_access_channels(websocketpp::log::alevel::all);
cl.clear_access_channels(websocketpp::log::alevel::all);
cl.init_asio();
// Setting callbacks
cl.set_message_handler(bind(&on_message, &cl, ::_1, ::_2));
cl.set_open_handler(bind(&on_open, &cl, ::_1));
cl.set_close_handler(bind(&on_close, &cl, ::_1));
cl.set_tls_init_handler(bind(&on_tls_init, string(address).c_str(), ::_1));
m_con.con = cl.get_connection(uri, ec);
if (ec) {
m_con.error = "Can't connect: " + ec.message();
}
cl.connect(m_con.con);
cout << "connected" << endl;
m_con.m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&run);
cout << "Thread created" << endl;
m_con.status = true;
} catch (websocketpp::exception const & e) {
cout << e.what() << endl;
}
}
我解决了这个问题。验证证书时出错。 修复它的代码:
context_ptr on_tls_init(const char* hostname, websocketpp::connection_hdl) {
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv12);
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);
ctx->set_verify_mode(boost::asio::ssl::verify_none);
} catch (std::exception& e) {
m_con.error = string(e.what());
}
return ctx;
}