Skip to content

Commit

Permalink
Merge pull request #22 from acgreek/websocketpp_example
Browse files Browse the repository at this point in the history
Websocketpp example
  • Loading branch information
lesismal committed Jun 18, 2021
2 parents 83b92ed + 91ee4a1 commit 99f20f6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/websocket_tls/client_websocketpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all: client

websocketpp:
git clone https://github.com/zaphoyd/websocketpp

client: websocketpp client.cpp
g++ -std=c++11 -L /usr/local/opt/openssl@1.1/lib -l ssl -lcrypto -lboost_system -lboost_chrono -lboost_random -I /usr/local/opt/openssl@1.1/include -I websocketpp -o $@ client.cpp

test: client
./client wss://echo.websocket.org

test_local: client
./client

.PHONY: all test test_local
84 changes: 84 additions & 0 deletions examples/websocket_tls/client_websocketpp/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>

#include <iostream>
#include <strings.h>

typedef websocketpp::client<websocketpp::config::asio_tls_client> client;

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;

void sendFunc(client* c,websocketpp::connection_hdl hdl) {
websocketpp::lib::error_code ec;
char *in = "Be careful when adding or removing oneof fields. If checking the value of a oneof returns None/NOT_SET, it could mean that the oneof has not been set or it has been set to a field in a different version of the oneof. There is no way to tell the difference, since there's no way to know if an unknown field on the wire is a member of the oneof.hello world";
c->send(hdl, in, strlen(in), websocketpp::frame::opcode::binary, ec);
if (ec) {
std::cout << "Echo failed because: " << ec.message() << std::endl;
}
}
// 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;
}
}

int main(int argc, char* argv[]) {
// Create a client endpoint
client c;
c.set_tls_init_handler([&c](websocketpp::connection_hdl){
return websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv12);
});

std::string uri = "wss://localhost:8888/wss";

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();

// Register our message handler
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
c.set_open_handler(bind(&sendFunc,&c,::_1));


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;
}
}

0 comments on commit 99f20f6

Please sign in to comment.