C++ C++;无法解压缩通过充气算法从WebSocket API(Okex)返回的消息
我正在尝试解压缩从WebSocket API(Okex)返回的消息。我尝试使用zlib解压,但失败了。请告知如何这样做 下面是编写的websocket类,用于连接到okex并从websocket服务器返回数据C++ C++;无法解压缩通过充气算法从WebSocket API(Okex)返回的消息,c++,C++,我正在尝试解压缩从WebSocket API(Okex)返回的消息。我尝试使用zlib解压,但失败了。请告知如何这样做 下面是编写的websocket类,用于连接到okex并从websocket服务器返回数据 #include <websocketpp/config/asio_client.hpp> #include <websocketpp/client.hpp> #include <iostream> typedef websocketpp::clie
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef std::shared_ptr<boost::asio::ssl::context> context_ptr;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
using websocketpp::lib::bind;
void on_open(client* c, websocketpp::connection_hdl hdl) {
std::string msg = "{\"op\": \"subscribe\", \"args\": [\"spot/depth5:ETH-USDT\"]}";
c->send(hdl,msg,websocketpp::frame::opcode::text);
c->get_alog().write(websocketpp::log::alevel::app, "Sent Message: "+msg);
}
void websocketClient::on_message(websocketpp::connection_hdl hdl, message_ptr msg) {
std::cout << msg->get_payload().size()<< std::endl;
std::cout << msg->get_payload().data()<< std::endl;
string str = utility::inflationAlgorithm::decompress(msg->get_payload());
}
websocketClient::websocketClient() {
client c;
std::string uri = "wss://real.okex.com:8443/ws/v3";
try {
// Set logging to be pretty verbose (everything except message payloads)
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize ASIO
c.init_asio();
c.set_tls_init_handler(bind(&on_tls_init));
// Register our message handler
c.set_open_handler(bind(&on_open, &c,::_1));
c.set_message_handler(bind(&websocketClient::on_message,this,::_1,::_2));
websocketpp::lib::error_code ec;
websocketClient::con = c.get_connection(uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
}else {
c.connect(websocketClient::con);
// Start the ASIO io_service run loop
// this will cause a single connection to be made to the server. c.run()
// will exit when this connection is closed.
c.run();
}
} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
}
}
#包括
#包括
#包括
typedef websocketpp::客户端;
typedef std::共享的上下文;
使用websocketpp::lib::占位符::\u 1;
使用websocketpp::lib::占位符::\u 2;
使用websocketpp::lib::bind;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
使用websocketpp::lib::bind;
打开时无效(客户端*c,websocketpp::连接\u hdl){
std::string msg=“{\'op\':\'subscribe\',\'args\':[\'spot/depth5:ETH-USDT\'”];
c->send(hdl、msg、websocketpp::frame::opcode::text);
c->get_alog().write(websocketpp::log::alevel::app,“发送消息:”+msg);
}
void websocketClient::on_消息(websocketpp::connection_hdl hdl,message_ptr msg){
std::无法获取_有效负载().size()
#include <sstream>
#include "inflationAlgorithm.h"
using namespace zlibcomplete;
using namespace std;
std::string utility::inflationAlgorithm::decompress(const std::string & str)
{
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (inflateInit2(&zs, 47) != Z_OK)
throw(std::runtime_error("inflateInit failed while decompressing."));
zs.next_in = (Bytef*)str.data();
zs.avail_in = (uint)str.size();
int ret;
char outbuffer[32768];
std::string outstring;
// get the decompressed bytes blockwise using repeated calls to inflate
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflateInit2(&zs, 47);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
std::ostringstream oss;
oss << "Exception during zlib decompression: (" << ret << ") "
<< zs.msg;
throw(std::runtime_error(oss.str()));
}
return outstring;
}