87 lines
3.0 KiB
C++
87 lines
3.0 KiB
C++
#include <websocketpp/config/asio_client.hpp>
|
|
#include <websocketpp/client.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
|
|
typedef std::shared_ptr<boost::asio::ssl::context> context_ptr;
|
|
using websocketpp::lib::placeholders::_1;
|
|
using websocketpp::lib::placeholders::_2;
|
|
using websocketpp::lib::bind;
|
|
|
|
// pull out the type of messages sent by our config
|
|
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
|
|
|
|
// This message handler will be invoked once for each incoming message. It
|
|
// prints the message and then sends a copy of the message back to the server.
|
|
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
|
|
std::cout << "on_message called with hdl: " << hdl.lock().get()
|
|
<< " and message: " << msg->get_payload()
|
|
<< std::endl;
|
|
|
|
|
|
websocketpp::lib::error_code ec;
|
|
|
|
c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
|
|
if (ec) {
|
|
std::cout << "Echo failed because: " << ec.message() << std::endl;
|
|
}
|
|
}
|
|
|
|
static context_ptr on_tls_init() {
|
|
// establishes a SSL connection
|
|
context_ptr ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
|
|
|
|
try {
|
|
ctx->set_options(boost::asio::ssl::context::default_workarounds |
|
|
boost::asio::ssl::context::no_sslv2 |
|
|
boost::asio::ssl::context::no_sslv3 |
|
|
boost::asio::ssl::context::single_dh_use);
|
|
} catch (std::exception &e) {
|
|
std::cout << "Error in context pointer: " << e.what() << std::endl;
|
|
}
|
|
return ctx;
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
// Create a client endpoint
|
|
client c;
|
|
|
|
std::string uri = "wss://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=websocket";
|
|
|
|
if (argc == 2) {
|
|
uri = argv[1];
|
|
}
|
|
|
|
try {
|
|
// Set logging to be pretty verbose (everything except message payloads)
|
|
c.set_access_channels(websocketpp::log::alevel::all);
|
|
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
|
|
|
|
// Initialize ASIO
|
|
c.init_asio();
|
|
c.set_tls_init_handler(bind(&on_tls_init));
|
|
|
|
// Register our message handler
|
|
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
|
|
|
|
websocketpp::lib::error_code ec;
|
|
client::connection_ptr con = c.get_connection(uri, ec);
|
|
if (ec) {
|
|
std::cout << "could not create connection because: " << ec.message() << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
// Note that connect here only requests a connection. No network messages are
|
|
// exchanged until the event loop starts running in the next line.
|
|
c.connect(con);
|
|
|
|
// Start the ASIO io_service run loop
|
|
// this will cause a single connection to be made to the server. c.run()
|
|
// will exit when this connection is closed.
|
|
c.run();
|
|
} catch (websocketpp::exception const & e) {
|
|
std::cout << e.what() << std::endl;
|
|
}
|
|
}
|