N
N
Nathanael2017-08-22 16:37:34
C++ / C#
Nathanael, 2017-08-22 16:37:34

Autobahn, how to unbox a complex type in C++?

I am subscribing to ticker events at wss://api.poloniex.com. The server is not mine, I just need to collect information from it, but I have a problem with the format.
I have this event and can't find the correct type for such a complex case:
RX message: event [543453838537363, 3807360795988603, {}, ["BTC_BCH", "0.18447297", "0.18447297", "0.18410001", "0.06427077", " 14359.75209355", "89775.00285182", 0, "0.19500000", "0.14000001"]]
Can anyone help me?

#include <autobahn/autobahn.hpp>
#include <autobahn/wamp_websocketpp_websocket_transport.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <boost/version.hpp>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include <typeinfo>
#include <sstream>


typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef autobahn::wamp_websocketpp_websocket_transport<websocketpp::config::asio_tls_client> websocket_transport;

void on_topic1(const autobahn::wamp_event& event)
{
    //std::cerr << "received event: " << event.argument<uint64_t>(0) << std::endl;
}

int main(int argc, char const *argv[])
{
    try {
        //std::cerr << "Connecting to realm: " << parameters->realm() << std::endl;

        boost::asio::io_service io;
        //bool debug = parameters->debug();

        client ws_client;
        ws_client.init_asio(&io);
        ws_client.set_tls_init_handler([&](websocketpp::connection_hdl) {
            return websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv12_client);
        });
        auto transport = std::make_shared < autobahn::wamp_websocketpp_websocket_transport<websocketpp::config::asio_tls_client> >(
                ws_client, "wss://api.poloniex.com:443", true);

        // create a WAMP session that talks WAMP-RawSocket over TCP
        auto session = std::make_shared<autobahn::wamp_session>(io, true);

        transport->attach(std::static_pointer_cast<autobahn::wamp_transport_handler>(session));

        // Make sure the continuation futures we use do not run out of scope prematurely.
        // Since we are only using one thread here this can cause the io service to block
        // as a future generated by a continuation will block waiting for its promise to be
        // fulfilled when it goes out of scope. This would prevent the session from receiving
        // responses from the router.
        boost::future<void> connect_future;
        boost::future<void> start_future;
        boost::future<void> join_future;
        boost::future<void> subscribe_future;
        connect_future = transport->connect().then([&](boost::future<void> connected) {
            try {
                connected.get();
            } catch (const std::exception& e) {
                std::cerr << e.what() << std::endl;
                io.stop();
                return;
            }

            std::cerr << "transport connected" << std::endl;

            start_future = session->start().then([&](boost::future<void> started) {
                try {
                    started.get();
                } catch (const std::exception& e) {
                    std::cerr << e.what() << std::endl;
                    io.stop();
                    return;
                }

                std::cerr << "session started" << std::endl;

                join_future = session->join("realm1").then([&](boost::future<uint64_t> joined) {
                    try {
                        std::cerr << "joined realm: " << joined.get() << std::endl;
                    } catch (const std::exception& e) {
                        std::cerr << e.what() << std::endl;
                        io.stop();
                        return;
                    }

                    subscribe_future = session->subscribe("ticker", &on_topic1).then([&] (boost::future<autobahn::wamp_subscription> subscribed)
                    {
                        try {
                            std::cerr << "subscribed to topic: " << subscribed.get().id() << std::endl;
                        }
                        catch (const std::exception& e) {
                            std::cerr << e.what() << std::endl;
                            io.stop();
                            return;
                        }

                    });
                });
            });
        });

        std::cerr << "starting io service" << std::endl;
        io.run();
        std::cerr << "stopped io service" << std::endl;
    }
    catch (std::exception& e) {
        std::cerr << "exception: " << e.what() << std::endl;
        //ret_var.successfully_ran = false;
        return 1;
    }


    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2017-08-22
@petermzg

This part "[543453838537363, 3807360795988603, {}, [" btc_bch "," 0.18447297 "," 0.18410001 "," 0.06427077 "," 14359.75209355 "," 89775.00285182 ", 0," 0.19500000 "," 0.14000001 "] ]" - JSon
And remove this "RX message: event" before parsing JSON

N
Nathanael, 2017-08-22
@nafe93

io.run(); Responsible for the output, while in addition to the RX message, much more is displayed.
Is it possible to somehow call a function from the autobahn library through io.
The function is buried in the wamp_websocket_transport.ipp file.
Here is a link to the library itself:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question