5

Poloniexの Push API に接続したい。彼らのページには、次のように書かれています。

プッシュ 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++ を使用します。Autobahn|CPPと呼ばれる C++ 用の Autobahn-Library もあります。私はそれをインストールし、サブスクライバーのサンプル コードを少し変更して実行しようとしました (基本的には、アドレスとポートをハードコーディングしただけです)。

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

ここで説明することがいくつかあります。IP アドレス173.236.42.218 は、 api.poloniex.comping するだけでわかりました。

ポート 443 は標準の SSL ポートです。8000 である標準の WAMP/WebSocket ポートを使用しようとしましたが、サーバーはそれを受け入れません。80も受け付けません。

したがって、プログラムを開始すると、出力は次のようになります。

ioサービスを開始しています

サーバーに接続しました

その後、何も起こりません。そのため、WS ハンドシェイクが実行されるsession_start()でコードをスタックする必要があります。これは、80 行目のwamp_session.ippを調べるとわかります。

私の意見では、問題は、API が安全な接続 (ws s ://) を使用したいということです。このコードは SSL で暗号化された接続を作成しようとしていないようで、安全な接続が必要であることをセッションに伝える方法がわかりません。

編集:この質問では、著者は、WebSocket プロトコルを使用する前に最初に http-requestをアップグレードする必要がある場合、Autobahn は混合 http/wamp サーバーを処理できないと述べています。Poloniex がこのような混合型を使用していることは知っていますが、Autobahn| で API にアクセスしようとしました。JSは既に正常に動作し、アップグレード リクエストも送信されます。多分これはアウトバーンです| CPPの問題?

編集 2:上記が当てはまる場合、Http-Update-Request を自分で送信し、SSL 暗号化を接続に入れることは可能ですか? ライブラリに干渉する可能性があるため、わかりません。

4

2 に答える 2

7

No, no, no this is a late response. Late or not though, I believe it may be a bit more direct a solution for you Bobface (and any others that struggled with this). I hesitate to give this out, as it will potentially be used by competitors. But, what's life without competition!? Boring, that's what. And moreover, I wish someone had come before me and posted this, so here you go! Be the change you wish to see, right?

Below, you'll find an implementation utilizing websocketpp and autobahn|cpp to connect to Poloniex's push api. In this case, it will receive updates made to the books for BTC_ETH.

Generally speaking, this is how you can utilize websocketpp and autobahn|cpp to connect to a secure web socket server implementing WAMP protocol (aka something like wss://ip-address.com:port).

Cheers!

Includes:

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

Code:

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;
}
于 2017-03-01T05:07:02.827 に答える
1

ご質問への回答がかなり遅れていることは承知しておりますが、リモート サーバーへの接続時に HTTP/Websocket のアップグレードを実行していないことが問題のようです。あなたが使用しているサンプル コードは、rawsocket_endpoint トランスポートでセットアップされています。これは、HTTP Websocket のアップグレードや Websocket のカプセル化が行われていないことを意味します。あなたの問題が SSL とは何の関係もないと思います。

Websocket 接続を機能させるには、 Websocketppを利用するAutobahnCPP の例を確認する必要があります。

于 2016-06-16T04:19:57.997 に答える