我想问 c++的 websocketpp库 连接币安的现货行情, 但是老报错, 求问是什么原因, ok的连接上了
这是连接的python代码
import websocket
import time
import threading
import json
def on_open(wsapp):
print("on_open")
input_msg = {
"method": "SUBSCRIBE",
"params": [
"btcusdt@depth"
],
"id": 1
}
print(input_msg)
input_msg = json.dumps(input_msg)
wsapp.send(input_msg)
# print("send:", input_msg)
# input()
# def send_message():
# for i in range(10):
# wsapp.send(f"Hello {i}")
# time.sleep(1)
# wsapp.close()
# threading.Thread(target=send_message).start()
def on_message(wsapp, message):
print("on_message:", message)
def on_close(wsapp):
print("on_close")
url = "wss://stream.binance.com:9443/stream?streams=BTCUSDT@ticker"
url = "wss://stream.binance.com:9443/stream"
#url = "wss://stream.binance.com:443/stream"
#url = "wss://data-stream.binance.vision"
wsapp = websocket.WebSocketApp(url,
on_open=on_open,
on_message=on_message,
on_close=on_close)
wsapp.run_forever()
我报错的c++代码
// #include <iostream>
// #include <thread>
// #include <vector>
// void thread_func() noexcept {
// std::cout << "thread_func start ..." << std::endl;
// std::vector<int> vec;
// vec.push_back(1);
// vec.push_back(2);
// std::cout << vec.at(1) << std::endl;
// }
// int main (void) {
// std::thread th1(thread_func);
// th1.join();
// return 0;
// }
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
void on_open(websocketpp::connection_hdl hdl, client* c) {
std::cout << "WebSocket connection opened!" << std::endl;
websocketpp::lib::error_code ec;
client::connection_ptr con = c->get_con_from_hdl(hdl, ec);
if (ec) {
std::cout << "Failed to get connection pointer: " << ec.message() << std::endl;
return;
}
//std::string payload = "{\"userKey\":\"API_KEY\", \"symbol\":\"EURUSD,GBPUSD\"}";
//std::string payload = "{\"method\":\"SUBSCRIBE\", \"params\":[\"btcusdt@depth\"], \"id\":1}";
//binance
std::string payload="{ \"method\": \"SUBSCRIBE\", \"params\": [ \"btcusdt@depth\"], \"id\": 1}";
//okex
//std::string payload = "{\"op\": \"subscribe\", \"args\": [{ \"channel\": \"books5\", \"instId\": \"BTC-USDT\"}]}";
c->send(con, payload, websocketpp::frame::opcode::text);
}
void on_message(websocketpp::connection_hdl, client::message_ptr msg) {
std::cout << "Received message: " << msg->get_payload() << std::endl;
}
void on_fail(websocketpp::connection_hdl hdl) {
std::cout << "WebSocket connection failed!" << std::endl;
}
void on_close(websocketpp::connection_hdl hdl) {
std::cout << "WebSocket connection closed!" << std::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::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 |
boost::asio::ssl::context::tlsv12_client );
ctx->set_verify_mode(boost::asio::ssl::verify_none);
} catch (std::exception& e) {
std::cout << "TLS Initialization Error: " << e.what() << std::endl;
}
return ctx;
}
int main(int argc, char* argv[]) {
client c;
//std::string uri = "wss://echo.websocket.org";
//std::string hostname = "echo.websocket.org";
// 币安的没跑通
std::string uri = "wss://stream.binance.com:9443/stream";
//std::string uri = "wss://stream.binance.com:443/stream";
std::string hostname = "stream.binance.com";
//std::string hostname = "128.242.240.91";
// 下面跑通了
// std::string uri = "wss://ws.okx.com:8443/ws/v5/public";
// std::string hostname = "ws.okx.com";
// -------
// std::string uri = "ws://124.222.224.186:8800";
// std::string hostname = "124.222.224.186";
// std::string hostname = "marketdata.tradermade.com/feedadv";
// std::string uri = "wss://" + hostname;
try {
// Configure WebSocket++ client
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
c.set_error_channels(websocketpp::log::elevel::all);
c.init_asio();
// Set message, TLS initialization, open, fail, and close handlers
c.set_message_handler(&on_message);
c.set_tls_init_handler(bind(&on_tls_init, hostname.c_str(), ::_1));
c.set_open_handler(bind(&on_open, ::_1, &c));
c.set_fail_handler(bind(&on_fail, ::_1));
c.set_close_handler(bind(&on_close, ::_1));
// Enable detailed error logging
c.set_error_channels(websocketpp::log::elevel::all);
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "Could not create connection because: " << ec.message() << std::endl;
return 0;
}
// Create a connection to the specified url
std::cout << "connect" << std::endl;
std::cout << "con:" << con->get_uri() << std::endl;
c.connect(con);
std::cout << "get_remote_endpoint:" << con->get_remote_endpoint() << std::endl;
std::cout << "run" << std::endl;
c.run();
} catch (websocketpp::exception const & e) {
std::cout << "WebSocket Exception: " << e.what() << std::endl;
}
return 0;
}