I'm trying to use websockets Mt. Gox API to get the recent quotes for the USD currency. So, i write the following code that connects to "ws://websocket.mtgox.com:80/mtgox" and listens for the messages.
#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;
}
}
When a message with channel == "d5f06780-30a8-4a48-a2f8-7ed181b4a13f" (ticker channel) received, i'm trying to parse it and get the ticker.buy.value and ticker.sell.value. But the values are far from their analogues from this statistics - http://bitcoincharts.com/markets/currency/USD.html (low and high matches only). What am I doing wrong? Maybe another channel, server, json field from message or smth else?