[英]Connecting to Poloniex Push-API
我想連接到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 ++。 還有一個用於C ++的Autobahn-Library,叫做Autobahn | CPP 。 我已經安裝了它並嘗試運行他們的訂閱者示例代碼 ,幾乎沒有修改(基本上只是硬編碼地址和端口):
#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服務
連接到服務器
然后,沒有任何反應。 所以代碼必須停留在session_start() ,在那里執行WS握手,當你在第80行查看wamp_session.ipp時可以看到的內容。
在我看來,問題是API想要使用安全連接(ws s ://)。 看起來這段代碼不會嘗試創建SSL加密連接,我不知道如何告訴會話我需要一個安全的連接。
編輯 :在這個問題中 ,作者說Autobahn無法處理混合的http / wamp服務器,並且在使用WebSocket協議之前首先需要升級 http-request。 我知道Poloniex使用這種混合類型,但我試圖用Autobahn訪問API JS已經和它在那里工作正常,也發送升級請求。 所以也許這是一輛高速公路 CPP問題?
編輯2:如果以上是真的,是否可以自己發送Http-Update-Request,甚至可能在連接上加上SSL加密? 我不確定,因為這可能會干擾圖書館。
不,不, 這不是遲到的回應。 遲到或者沒有,我相信這對你來說可能更直接一個解決方案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;
}
我知道這是對你的問題的一個相當晚的回應,但問題是你在連接到遠程服務器時沒有執行HTTP / Websocket升級。 您正在使用的示例代碼是使用rawsocket_endpoint傳輸進行設置的,我猜這意味着沒有HTTP Websocket升級或Websocket封裝發生。 我不相信你的問題與SSL有關。
要使Websocket連接正常工作,您應該查看使用Websocketpp的AutobahnCPP示例 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.