连接到Poloniex Push-API [英] Connecting to Poloniex Push-API
问题描述
我想连接到 Poloniex的Push API .他们在他们的页面上写了以下内容:
I want to connect to the Push API of Poloniex. On their page they write the following:
为了使用推入API,请连接到wss://api.poloniex.com并订阅所需的提要.
In order to use the push API, connect to wss://api.poloniex.com and subscribe to the desired feed.
wss = WebSocket安全->受SSL保护
他们还给出了Node.js和Autobahn | JS的示例:
They also give an example for Node.js and 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 ++的高速公路库,称为高速公路| CPP .我已经安装了它,并尝试运行其用户示例代码几乎没有修改(基本上只是对地址和端口进行了硬编码):
However, I do not want to use JavaScript, instead I use C++. There's also a Autobahn-Library for C++, called Autobahn|CPP. I've installed it and tried to run their subscriber example code with little modifications (basically just hardcoded the adress and port):
#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地址 173.236.42.218 .
There are a few things to explain here: I found out the IP-Adress 173.236.42.218 by simply pinging api.poloniex.com.
端口443是标准的SSL端口.我尝试使用标准的WAMP/WebSocket端口8000,但服务器不接受.也不接受80.
Port 443 is the standard SSL-Port. I've tried using the standard WAMP/WebSocket port which is 8000, but the server does not accept that. 80 is also not accepted.
因此,如果我启动程序,输出如下:
So if I start the program, the output is the following:
启动io服务
starting io service
已连接到服务器
然后,什么都没有发生.因此,代码必须停留在执行WS握手的 session_start()上,当您查看
Then, nothing happens. So the code must be stuck at session_start(), where the WS handshake is performed, what you can see when you look into wamp_session.ipp at line 80.
我认为问题在于API希望使用安全连接(ws s ://).似乎这段代码没有尝试创建SSL加密的连接,而且我不知道如何告诉会话我需要一个安全的连接.
In my opinion the problem is that the API wants to use a secure connection (wss://). It seems like this piece of code does not try to create a SSL-encrypted connection and I don't know how to tell the session that I need a secure one.
编辑:在此问题中表示,在使用WebSocket协议之前,Autobahn无法处理首先需要 upgrade http-request的混合http/wamp服务器.我知道Poloniex使用了这种混合类型,但是我已经尝试使用Autobahn | JS 访问API,并且在那里工作正常,并且还发送升级请求.因此,也许这是一个高速公路| CPP 问题?
Edit: In this question the Author says that Autobahn can't handle mixed http/wamp servers where and upgrade http-request is required first before using the WebSocket protocol. I know Poloniex uses such a mixed type, but I've tried to access the API with Autobahn|JS already and there it works fine, also sending the upgrade request. So maybe this is a Autobahn|CPP issue?
如果以上内容是正确的,是否可以自己发送Http-Update-Request,甚至可以对连接进行SSL加密?我不确定,因为这可能会干扰图书馆.
Edit 2: If the above is true, is it possible to send the Http-Update-Request myself and maybe even put a SSL-encryption onto the connection? I'm not sure because maybe that would interfere the library.
推荐答案
不,不,不,这是较晚的响应.迟到还是迟到,我相信这可能会为您 Bobface (以及其他任何遇到麻烦的人)提供更直接的解决方案这).我不愿透露这个信息,因为它可能会被竞争对手使用.但是,没有竞争的生活是什么!?无聊的就是这样.而且,我希望有人在我之前发布了这个,所以就到这里!是您希望看到的变化,对吗?
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?
下面,您将找到一个使用websocketpp和autobahn | cpp连接到Poloniex的push api的实现.在这种情况下,它将收到针对BTC_ETH的书籍更新.
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.
通常来说,这就是您可以如何使用websocketpp和autobahn | cpp连接到实现WAMP协议(又称为wss://ip-address.com:port之类)的安全Web套接字服务器.
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).
干杯!
包括:
#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;
}
这篇关于连接到Poloniex Push-API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!