I am attempting to set up a websocket connection with a remote server and get the following error: The WebSocket handshake was declined by the remote peer
I am following this example: https://www.boost.org/doc/libs/1_70_0/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp
For the following server: https://api.hitbtc.com/#socket-api-reference
Thanks for your help in advance!
int main(int argc, char** argv) {
try
{
auto const host = "api.hitbtc.com";
auto const port = "443";
net::io_context ioc;
tcp::resolver resolver{ ioc };
websocket::stream<tcp::socket> ws{ ioc };
auto const results = resolver.resolve(host, port);
net::connect(ws.next_layer(), results.begin(), results.end());
// Perform the websocket handshake - this is where i presume the issue is.
ws.handshake(host, "/api/2/ws");
beast::flat_buffer buffer;
ws.read(buffer);
ws.close(websocket::close_code::normal);
std::cout << beast::make_printable(buffer.data()) << std::endl;
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
You have to provide ssl context. Here is example from boost.org,
and here is example for api.hitbtc handshake and getting Currency info via JSON-RPC (I've omitted certificate loading):
You should get stringified JSON-RPC response. In this case for getCurrency("ETH") request, the response would be: