adds utility client example

This commit is contained in:
Peter Thorson
2013-05-03 17:08:05 -05:00
parent 64f7e24b4d
commit 28eecb80e0
3 changed files with 89 additions and 0 deletions

View File

@@ -217,6 +217,9 @@ broadcast_server = SConscript('#/examples/broadcast_server/SConscript',variant_d
# echo_client
echo_client = SConscript('#/examples/echo_client/SConscript',variant_dir = builddir + 'echo_client',duplicate = 0)
# utility_client
utility_client = SConscript('#/examples/utility_client/SConscript',variant_dir = builddir + 'utility_client',duplicate = 0)
# subprotocol_server
subprotocol_server = SConscript('#/examples/subprotocol_server/SConscript',variant_dir = builddir + 'subprotocol_server',duplicate = 0)

View File

@@ -0,0 +1,23 @@
## Utility client example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is avaliable build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('utility_client', ["utility_client.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system','regex','random'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('utility_client', ["utility_client.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,63 @@
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_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 print_handler(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
c->get_alog().write(websocketpp::log::alevel::app,msg->get_payload());
}
void write_on_open(client* c, websocketpp::connection_hdl hdl) {
c->send(hdl, "ping", websocketpp::frame::opcode::text);
}
int main(int argc, char* argv[]) {
// Create a server endpoint
client endpoint;
std::string uri = "ws://localhost:9001";
if (argc == 2) {
uri = argv[1];
}
try {
// We expect there to be a lot of errors, so suppress them
endpoint.set_access_channels(websocketpp::log::alevel::all);
endpoint.set_error_channels(websocketpp::log::elevel::all);
// Initialize ASIO
endpoint.init_asio();
// Register our handlers
endpoint.set_message_handler(bind(&print_handler,&endpoint,::_1,::_2));
endpoint.set_open_handler(bind(&write_on_open,&endpoint,::_1));
websocketpp::lib::error_code ec;
client::connection_ptr con = endpoint.get_connection(uri, ec);
con->set_proxy("http://humupdates.uchicago.edu:8443");
endpoint.connect(con);
// Start the ASIO io_service run loop
endpoint.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}