I want to connect to the Poloniex Push API . On their page they write the following:
To use the push API, connect to wss: //api.poloniex.com and subscribe to the desired channel.
wss = WebSocket Secure -> Secure SSL
They also provide 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();
However, I do not want to use JavaScript, instead I use C ++. There is also an Autobahn-Library for C ++ called Autobahn | CPP I installed it and tried to run the sample subscriber code with a few changes (basically just a hard-coded address 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; }
There are a few things to explain here: I found out the IP address 173.236.42.218, just ping api.poloniex.com.
Port 443 is a standard SSL port. I tried using the standard WAMP / WebSocket port, which is 8000, but the server does not accept this. 80 is also not accepted.
So, if I run the program, the output is as follows:
launch io service
connected to server
Then nothing happens. So the code should get stuck in session_start () where the WS handshake is done, which you can see when viewing wamp_session.ipp on line 80.
In my opinion, the problem is that the API wants to use a secure connection (ws s : //). It seems that this piece of code is not trying to create an SSL encrypted connection, and I donβt know how to tell the session that I need a secure one.
Change In this question, the Author says that Autobahn cannot handle mixed http / wamp servers, where you must first use the HTTP request before using the WebSocket protocol. I know that Poloniex uses such a mixed type, but I already tried to access the API using Autobahn | JS and there it works fine, and also sends an update request. So maybe this is a problem with CPP autorun?
Edit 2: If this is true, is it possible to send an Http-Update-Request and maybe even set up SSL encryption in the connection? I'm not sure, because maybe this will affect the library.