我正在尝试连接到 Kraken websocket API。但我收到“WebSocket 握手被远程对等方拒绝”错误。
我已经为 websocket 和交易所的 rest api 客户端编写了一个包装类。它与 Binance websocket API 配合得很好,但是 Kraken websocket 连接不成功。
我也尝试过不同类型的 tls(ssl::context ctx_webSocket{ ssl::context::tlsv13_client };) 版本,但结果是一样的。
class Exchange {
public:
Exchange(std::string name, const std::string& http_host) :m_name(std::move(name)) {
init_http(http_host);
}
void init_http(std::string const& host) {
const auto results{ resolver.resolve(host,"443") };
get_lowest_layer(stream).connect(results);
if (!SSL_set_tlsext_host_name(stream.native_handle(), host.c_str()))
{
boost::system::error_code ec{ static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category() };
throw boost::system::system_error{ ec };
}
stream.handshake(ssl::stream_base::client);
}
void init_webSocket(std::string const& host, std::string const& port, std::string const& target) {
auto const results = resolver_webSocket.resolve(host, port);
net::connect(ws.next_layer().next_layer(), results.begin(), results.end());
ws.next_layer().handshake(ssl::stream_base::client);
// Set a decorator to change the User-Agent of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
ws.handshake(host, target.c_str());
}
void read_Socket() {
ws.read(buffer);
}
void write_Socket(const std::string& text) {
ws.write(net::buffer(text));
}
std::string get_socket_data() {
return beast::buffers_to_string(buffer.data());
}
void buffer_clear() {
buffer.clear();
}
void webSocket_close() {
ws.close(websocket::close_code::none);
}
private:
// HTTP REQUEST SET //
std::string m_name;
net::io_context ioc;
ssl::context ctx{ ssl::context::tlsv12_client };
tcp::resolver resolver{ ioc };
Stream stream{ ioc, ctx };
// WEB SOCKET SET //
std::string m_web_socket_host;
std::string m_web_socket_port;
beast::flat_buffer buffer;
net::io_context ioc_webSocket;
ssl::context ctx_webSocket{ ssl::context::tlsv13_client };
tcp::resolver resolver_webSocket{ ioc_webSocket };
websocket::stream<beast::ssl_stream<tcp::socket>> ws{ ioc_webSocket, ctx_webSocket };
};
int main()
{
Exchange kraken{ "kraken","api.kraken.com" };
try
{
kraken.init_webSocket("ws.kraken.com", "443", "/");
while (true)
{
kraken.read_Socket();
std::cout << kraken.get_socket_data();
return 1;
kraken.buffer_clear();
}
kraken.webSocket_close();
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
}