连接到 Poloniex Push-API
Connecting to Poloniex Push-API
我想连接到 Push API of Poloniex。在他们的页面上,他们写了以下内容:
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 的示例:
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. I've installed it and tried to run their subscriber example code,稍作修改(基本上只是硬编码地址和端口):
#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
端口 443 是标准的 SSL 端口。我试过使用标准 WAMP/WebSocket 端口 8000,但服务器不接受。 80也不接受。
所以如果我启动程序,输出如下:
starting io service
connected to server
然后,什么也没有发生。所以代码肯定卡在了session_start(),这里是执行WS握手的地方,看第80行的wamp_session.ipp就可以看到。
在我看来,问题在于 API 想要使用安全连接 (wss://)。这段代码似乎没有尝试创建 SSL 加密连接,我不知道如何告诉会话我需要一个安全连接。
编辑:在this question中,作者说高速公路无法处理混合http/wamp服务器,其中升级 在使用 WebSocket 协议之前首先需要 http-request。我知道 Poloniex 使用这种混合类型,但我已经尝试使用 Autobahn|JS 访问 API 并且它工作正常,还发送了升级请求。所以也许这是一个高速公路|CPP问题?
编辑 2: 如果上述内容属实,是否可以自己发送 Http-Update-Request,甚至可以对连接进行 SSL 加密?我不确定,因为这可能会干扰图书馆。
我知道这是对您问题的较晚答复,但问题似乎是您在连接到远程服务器时没有执行 HTTP/Websocket 升级。您使用的示例代码是使用 rawsocket_endpoint 传输设置的,我猜这意味着没有 HTTP Websocket 升级或 Websocket 封装发生。我认为您的问题与 SSL 无关。
要使 Websocket 连接正常工作,您应该查看 AutobahnCPP example that makes use of Websocketpp。
不,不,不 这 是一个迟到的回应。不管晚不晚,我相信这对您 Bobface(以及任何其他为此苦苦挣扎的人)来说可能是更直接的解决方案。我犹豫要不要公开它,因为它可能会被竞争对手使用。但是,没有竞争的生活还算什么!?无聊,就是这样。而且,我希望有人在我之前来发布这个,所以给你!成为您希望看到的变化,对吗?
下面,您将找到一个利用 websocketpp 和 autobahn|cpp 连接到 Poloniex 推送的实现 api。在这种情况下,它将接收对 BTC_ETH 的书籍所做的更新。
一般来说,这就是您如何利用 websocketpp 和 autobahn|cpp 连接到实现 WAMP 协议(又名类似 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;
}
我想连接到 Push API of Poloniex。在他们的页面上,他们写了以下内容:
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 的示例:
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. I've installed it and tried to run their subscriber example code,稍作修改(基本上只是硬编码地址和端口):
#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端口 443 是标准的 SSL 端口。我试过使用标准 WAMP/WebSocket 端口 8000,但服务器不接受。 80也不接受。
所以如果我启动程序,输出如下:
starting io service
connected to server
然后,什么也没有发生。所以代码肯定卡在了session_start(),这里是执行WS握手的地方,看第80行的wamp_session.ipp就可以看到。
在我看来,问题在于 API 想要使用安全连接 (wss://)。这段代码似乎没有尝试创建 SSL 加密连接,我不知道如何告诉会话我需要一个安全连接。
编辑:在this question中,作者说高速公路无法处理混合http/wamp服务器,其中升级 在使用 WebSocket 协议之前首先需要 http-request。我知道 Poloniex 使用这种混合类型,但我已经尝试使用 Autobahn|JS 访问 API 并且它工作正常,还发送了升级请求。所以也许这是一个高速公路|CPP问题?
编辑 2: 如果上述内容属实,是否可以自己发送 Http-Update-Request,甚至可以对连接进行 SSL 加密?我不确定,因为这可能会干扰图书馆。
我知道这是对您问题的较晚答复,但问题似乎是您在连接到远程服务器时没有执行 HTTP/Websocket 升级。您使用的示例代码是使用 rawsocket_endpoint 传输设置的,我猜这意味着没有 HTTP Websocket 升级或 Websocket 封装发生。我认为您的问题与 SSL 无关。
要使 Websocket 连接正常工作,您应该查看 AutobahnCPP example that makes use of Websocketpp。
不,不,不 这 是一个迟到的回应。不管晚不晚,我相信这对您 Bobface(以及任何其他为此苦苦挣扎的人)来说可能是更直接的解决方案。我犹豫要不要公开它,因为它可能会被竞争对手使用。但是,没有竞争的生活还算什么!?无聊,就是这样。而且,我希望有人在我之前来发布这个,所以给你!成为您希望看到的变化,对吗?
下面,您将找到一个利用 websocketpp 和 autobahn|cpp 连接到 Poloniex 推送的实现 api。在这种情况下,它将接收对 BTC_ETH 的书籍所做的更新。
一般来说,这就是您如何利用 websocketpp 和 autobahn|cpp 连接到实现 WAMP 协议(又名类似 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;
}