连接到Poloniex Push-API

时间:2015-12-26 00:34:23

标签: c++ api autobahn wamp-protocol autobahnws

我想连接到Push API of Poloniex。在他们的页面上,他们写下以下内容:

  

要使用推送API,请连接到wss://api.poloniex.com并订阅所需的订阅源。

wss = WebSocket Secure - >受SSL保护

他们还举例说明了Node.js和Autobahn | JS:

var autobahn = require('autobahn');
var wsuri = "wss://api.poloniex.com";
var connection = new autobahn.Connection({
  url: wsuri,
  realm: "realm1"
});

connection.onopen = function (session) {
        function marketEvent (args,kwargs) {
                console.log(args);
        }
        function tickerEvent (args,kwargs) {
                console.log(args);
        }
        function trollboxEvent (args,kwargs) {
                console.log(args);
        }
        session.subscribe('BTC_XMR', marketEvent);
        session.subscribe('ticker', tickerEvent);
        session.subscribe('trollbox', trollboxEvent);
}

connection.onclose = function () {
  console.log("Websocket connection closed");
}

connection.open();

但是,我不想使用JavaScript,而是使用C ++。还有一个用于C ++的高速公路图书馆,名为Autobahn|CPP。我已经安装了它并试图在没有任何修改的情况下运行subscriber example code(基本上只是对地址和端口进行了硬编码):

#include <autobahn/autobahn.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <memory>
#include <tuple>

void topic1(const autobahn::wamp_event& event)
{
    std::cerr << "received event: " << event.argument<uint64_t>(0) << std::endl;
}
using namespace boost;
using namespace boost::asio;
using namespace boost::asio::ip;
int main()
{
    try {

        boost::asio::io_service io;
        boost::asio::ip::tcp::socket socket(io);

        bool debug = true;
        auto session = std::make_shared<
                autobahn::wamp_session<boost::asio::ip::tcp::socket,
                boost::asio::ip::tcp::socket>>(io, socket, socket, debug);

        boost::future<void> start_future;
        boost::future<void> join_future;

        boost::asio::ip::tcp::endpoint rawsocket_endpoint( boost::asio::ip::address::from_string("173.236.42.218"), 443/*8000=standard*/);


        socket.async_connect(rawsocket_endpoint,
            [&](boost::system::error_code ec) {
                if (!ec) {
                    std::cerr << "connected to server" << std::endl;

                    start_future = session->start().then([&](boost::future<bool> started) {
                        if (started.get()) {
                            std::cerr << "session started" << std::endl;
                            join_future = session->join("realm1").then([&](boost::future<uint64_t> s) {
                                std::cerr << "joined realm: " << s.get() << std::endl;
                                session->subscribe("ticker", &topic1);
                            });
                        } else {
                            std::cerr << "failed to start session" << std::endl;
                            io.stop();
                        }
                    });
                } else {
                    std::cerr << "connect failed: " << ec.message() << std::endl;
                    io.stop();
                }
            }
        );

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

    return 0;
}

这里有几点需要解释: 我通过简单地ping api.poloniex.com 找到了IP-Adress 173.236.42.218

端口443是标准SSL端口。我尝试使用8000的标准WAMP / WebSocket端口,但服务器不接受。 80也不被接受。

因此,如果我启动该程序,输出如下:

  

启动io服务

     

连接到服务器

然后,没有任何反应。所以代码必须停留在执行WS握手的 session_start(),当您在第80行查看wamp_session.ipp时可以看到的内容。

在我看来,问题是API想要使用安全连接(ws s ://)。看起来这段代码并没有尝试创建一个SSL加密的连接,我也不知道如何告诉会话我需要一个安全的连接。

修改:在this question作者说,高速公路无法处理混合的http / wamp服务器,并且首先需要升级 http-request在使用WebSocket协议之前。我知道Poloniex使用这种混合类型,但我已经尝试使用Autobahn | JS 来访问API,并且它工作正常,也发送了升级请求。那么这可能是高速公路| CPP 问题吗?

编辑2:如果上述情况属实,是否可以自行发送Http-Update-Request,甚至可能在连接上加上SSL加密?我不确定,因为这可能会干扰图书馆。

2 个答案:

答案 0 :(得分:7)

不,不,不是迟到的回复。迟到或者没有,我相信这对你来说可能更直接一个解决方案Bobface(以及其他任何与之斗争的人)。我不愿意这样做,因为竞争对手可能会使用它。但是,没有竞争的生活是什么!?无聊,就是这样。此外,我希望有人来到我面前发布,所以你走了!是你想看的改变,对吧?

下面,您将找到一个利用websocketpp和autobahn | cpp连接到Poloniex的push api的实现。在这种情况下,它将收到BTC_ETH的书籍更新。

一般来说,这就是如何利用websocketpp和autobahn | cpp连接到实现WAMP协议的安全Web套接字服务器(也就是wss://ip-address.com:port)。

干杯!

<强>包括:

#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>

<强>代码:

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

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("BTC_ETH", &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 ret_var;
}

答案 1 :(得分:1)

我知道这是对你的问题的一个相当晚的回复,但问题是你在连接到远程服务器时没有执行HTTP / Websocket升级。您正在使用的示例代码是使用rawsocket_endpoint传输进行设置的,我猜这意味着没有HTTP Websocket升级或Websocket封装发生。我不相信你的问题与SSL有关。

要使Websocket连接正常工作,您应该查看使用AutobahnCPP exampleWebsocketpp