C++ 连接到Poloniex推送API

C++ 连接到Poloniex推送API,c++,api,autobahn,wamp-protocol,autobahnws,C++,Api,Autobahn,Wamp Protocol,Autobahnws,我想连接到网络。他们在页面上写下以下内容: 要使用推送API,请连接到wss://api.poloniex.com 并订阅所需的提要 wss=WebSocket安全->受SSL保护 他们还举了Node.js和Autobahn | js的一个例子: var autobahn = require('autobahn'); var wsuri = "wss://api.poloniex.com"; var connection = new autobahn.Connection({ url: ws

我想连接到网络。他们在页面上写下以下内容:

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

wss=WebSocket安全->受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++的自动库,叫做。我已经安装了它,并尝试运行它们,只需稍加修改(基本上只是硬编码地址和端口):

#包括在第80行

在我看来,问题在于API希望使用安全连接(wss:/)。这段代码似乎没有尝试创建SSL加密的连接,我不知道如何告诉会话我需要一个安全的连接

编辑:作者在文章中说,Autobahn无法处理混合http/wamp服务器,在使用WebSocket协议之前,首先需要对http请求进行升级。我知道Poloniex使用的是这样一种混合类型,但我已经尝试使用Autobahn |JS来访问API,在那里它工作正常,还发送了升级请求。那么这可能是高速公路的CPP问题


编辑2:如果上述情况属实,是否可以自己发送Http更新请求,甚至可能对连接进行SSL加密?我不确定,因为这可能会干扰库。

我知道这是对您的问题的一个比较晚的回答,但问题似乎是您在连接到远程服务器时没有执行HTTP/Websocket升级。您正在使用的示例代码是使用rawsocket_端点传输设置的,我猜这意味着不存在HTTP Websocket升级或Websocket封装。我认为您的问题与SSL无关

要使Websocket连接正常工作,您应该查看使用的。不,不,不,这是一个延迟响应。不管是晚还是晚,我相信这可能是一个更直接的解决方案,为你(和任何其他挣扎在这个问题上的人)。我不敢透露,因为它可能会被竞争对手使用。但是,没有竞争的生活是什么!?无聊,就是这样。而且,我希望有人能在我之前发布这篇文章,所以给你!这是你想看到的变化,对吗

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

一般来说,这就是如何利用websocketpp和autobahn | cpp连接到实现WAMP协议(又称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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
代码:

#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;
}
typedefwebsocketpp::客户端;
typedef autobahn::wamp_websocketpp_websocket_运输websocket_运输;
试一试{
//标准:cerr(
ws_客户端,”wss://api.poloniex.com:443“,对);
//创建通过TCP与WAMP RawSocket对话的WAMP会话
自动会话=std::使_共享(io,true);
传输->附加(std::static_pointer_cast(会话));
//确保我们使用的延续期货不会过早超出范围。
//由于我们在这里只使用一个线程,这可能会导致io服务阻塞
//作为一个延续所产生的未来,将阻止等待其承诺的实现
//在超出范围时完成。这将阻止会话接收
//来自路由器的响应。
boost::未来连接未来;
boost::未来开始未来;
未来加入未来;
boost::未来订阅未来;
connect_future=transport->connect()。然后([&](boost::future connected){
试一试{
已连接。get();
}捕获(const std::exception&e){

std::cerr连接通常未建立..立即,只是第三次…是的,我注意到最近使用此解决方案(以及我用node js和Java编写的其他解决方案)。如果有人对此有解决方案(除了让poloniex获得更快的ssl握手(响应)之外),那么请让我知道。是否有可能增加一个设置…以某种方式等待握手的时间?是的,是的,这是余震。派对有点晚了,但我很想从更有经验的主管那里得到一些帮助-我似乎在加入这个领域时遇到了一个问题-我遇到了句柄读取帧错误(TLS短读取).有什么线索吗?