C ++连接到poloniex推送WebSocket

时间:2018-06-21 18:33:41

标签: c++ websocket wamp push poloniex

我正在尝试连接至Poloniex wamp push。 使用高速公路cpp和boost 目前我正在使用此代码

#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 {
        boost::asio::io_service io;

    client ws_client;
    ws_client.init_asio(&io);
    ws_client.set_tls_init_handler([this](websocketpp::connection_hdl) {
        return websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv12_client);
    });

    auto transport = std::make_shared <websocket_transport>(
        ws_client, "wss://api2.poloniex.com", 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;
    return;
}

代码连接正常,我收到了

WebSocket Connection 104.20.12.48:443 v-2 Websocket++/0.7.0 / 101 
transport connectes 
session started" 

但是在那之后我收到了

[error] handle_read_frame error: websocketpp.transport:8 (TLS short read)
[info] asio async_write error: asio.ssl:336396495 (protocol is shutdown)
[fatal] handle_write_frame error: websocketpp.transport:2 (Underlying Transport Error)
[disconnect] Disconnect close local: [1006, Underlying Transport Error] remote:[1007]

我在另一篇文章中读到,这就是您可以如何利用websocketpp和autobahn | cpp连接到实现WAMP协议(又称为wss://ip-address.com:port之类)的安全Web套接字服务器。 但这似乎并不能解决问题

有什么建议吗?

谢谢

0 个答案:

没有答案