1

我正在尝试使用 websockets Mt. Gox API 来获取美元货币的最新报价。因此,我编写了以下代码连接到“ws://websocket.mtgox.com:80/mtgox”并监听消息。

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>

#include <boost/property_tree/json_parser.hpp>

#include <cstdlib>
#include <iostream>
#include <map>
#include <string>

typedef websocketpp::client<websocketpp::config::asio_client> websocket_client_t;
typedef websocketpp::config::asio_client::message_type::ptr websocket_message_ptr_t;

const std::string WEBSOCKET_MTGOX_API_BASE_URL = "ws://websocket.mtgox.com:80/mtgox";
const std::string ORIGIN_HEADER_VALUE = "http://www.example.com";
const std::string TICKER_CHANNEL_ID = "d5f06780-30a8-4a48-a2f8-7ed181b4a13f";

boost::property_tree::ptree construct_pt_from_string(const std::string& json_string)
{
    boost::property_tree::ptree pt;
    std::stringstream json_isstr;
    json_isstr << json_string;

    boost::property_tree::read_json(json_isstr, pt);

    return pt;
}

bool is_ticker_channel(const boost::property_tree::ptree& pt)
{
    try
    {
        const std::string& channel_id = pt.get<std::string>("channel");
        if (channel_id != TICKER_CHANNEL_ID)
        {
            return false;
        }
    }
    catch (const boost::property_tree::ptree_error& ex)
    {
        return false;
    }

    return true;
}

void parse_message(const std::string& message)
{
    boost::property_tree::ptree message_pt;
    try
    {
        message_pt = construct_pt_from_string(message);
    }
    catch (const boost::property_tree::json_parser_error& ex)
    {
        std::cerr << "An error occurred while parsing message: " << ex.what() << '\n';
        return;
    }

    if (!is_ticker_channel(message_pt))
    {
        return;
    }

    try
    {
        const std::string& currency = message_pt.get<std::string>("ticker.buy.currency");
        double buy = message_pt.get<double>("ticker.buy.value");
        double sell = message_pt.get<double>("ticker.sell.value");

        std::cout << "Currency: " << currency << " | Buy: " << buy << " | Sell: " << sell << '\n';
    }
    catch (const boost::property_tree::ptree_error& ex)
    {
        std::cerr << "An error occurred while parsing message: " << ex.what() << '\n';
        return;
    }
}

void on_websocket_open(websocketpp::connection_hdl hdl)
{
    std::cout << "WebSocket successfully opened \n";
}

void on_websocket_close(websocketpp::connection_hdl hdl)
{
    std::cout << "WebSocket closed \n";
}

void on_websocket_message(websocket_client_t* c, websocketpp::connection_hdl hdl, websocket_message_ptr_t msg)
{
    const std::string& message = msg->get_payload();
    parse_message(message);
}

int main()
{
    try
    {
        websocket_client_t c;

        c.init_asio();

        c.set_open_handler(on_websocket_open);
        c.set_close_handler(on_websocket_close);
        c.set_message_handler(
            websocketpp::lib::bind(
                &on_websocket_message
                , &c
                , websocketpp::lib::placeholders::_1
                , websocketpp::lib::placeholders::_2
            )
        );

        websocketpp::lib::error_code error_code;
        websocket_client_t::connection_ptr con = c.get_connection(WEBSOCKET_MTGOX_API_BASE_URL + "?Currency=USD", error_code);
        if (error_code)
        {
            std::cerr << "An error occurred while using function get_connection: " << error_code.message() << '\n';
            return EXIT_FAILURE;
        }
        con->replace_header("Origin", ORIGIN_HEADER_VALUE);

        c.connect(con);

        c.run();
    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
    catch (const websocketpp::lib::error_code& e)
    {
        std::cout << e.message() << std::endl;
    }
}

当收到带有 channel == "d5f06780-30a8-4a48-a2f8-7ed181b4a13f" (股票频道)的消息时,我正在尝试解析它并获取ticker.buy.value和ticker.sell.value。但这些值与此统计数据中的类似值相差甚远 - http://bitcoincharts.com/markets/currency/USD.html(仅限低匹配和高匹配)。我究竟做错了什么?也许来自消息的另一个频道、服务器、json 字段或其他东西?

4

0 回答 0