Skip to content

Instantly share code, notes, and snippets.

@matthill
Created April 12, 2016 03:04
Show Gist options
  • Save matthill/0bf049fa852d2f161a5ce41ee3e9511a to your computer and use it in GitHub Desktop.
Save matthill/0bf049fa852d2f161a5ce41ee3e9511a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <asio/ssl/context.hpp>
#define ASIO_STANDALONE 1
#include "websocketpp/config/asio_client.hpp"
#include "websocketpp/config/debug_asio.hpp"
#include "websocketpp/client.hpp"
//#include "websocketpp/transport/asio/security/tls.hpp"
typedef websocketpp::client<websocketpp::config::debug_asio_tls> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using namespace std;
// 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;
std::string response = "I'm here!asdfasdfasdfasdfasdfasdfasdfasdf";
c->send(hdl, response, websocketpp::frame::opcode::BINARY, ec);
if (ec) {
std::cout << "Echo failed because: " << ec.message() << std::endl;
}
}
bool verify_callback(
bool preverified, // True if the certificate passed pre-verification.
asio::ssl::verify_context& ctx // The peer certificate and other context.
)
{
return true;
}
websocketpp::lib::shared_ptr<asio::ssl::context> on_client_tls_init(
websocketpp::connection_hdl
)
{
cout << "Initializing TLS handler" << endl;
auto ctx = websocketpp::lib::make_shared<asio::ssl::context>(
asio::ssl::context::tlsv1_client);
// ... configure ctx as desired
ctx->set_verify_callback(verify_callback);
//ctx->set_verify_mode(asio::ssl::verify_none);
return ctx;
}
int main( int argc, const char** argv )
{
cout << "HELLO WORLD!" << endl;
std::string uri = "wss://localhost:8000/";
client c;
try {
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
c.set_tls_init_handler(bind(&on_client_tls_init,::_1));
c.init_asio();
// 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.
cout << "You are secure: " << c.is_secure() << endl;
cout << "Endpoint is listening: " << c.is_listening() << endl;
cout << "I am the server: " << c.is_server() << endl;
c.run();
cout << "Done running!" << endl;
} catch (websocketpp::exception const & e) {
cout << "ERROR handler exception" << endl;
std::cout << e.what() << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment