When I connected to localhost it worked. But when I'm trying connect to wss://echo.websocket.org it says:
[2020-07-03 00:24:19] [connect] Successful connection
[2020-07-03 00:24:20] [error] handle_transport_init received error: TLS handshake failed
[2020-07-03 00:24:20] [fail] WebSocket Connection 174.129.224.73:443 - "" / 0 websocketpp.transport.asio.socket:8 TLS handshake failed
[2020-07-03 00:24:20] [info] asio async_shutdown error: asio.ssl:336462231 (shutdown while in init)
What I need to do to make it works?
Parts of code:
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;
}
}
I solved this question. There was error with verifying certificates. Code to fix it:
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;
}