diff --git a/SConstruct b/SConstruct index 96026a449f..c9a6f8f85b 100644 --- a/SConstruct +++ b/SConstruct @@ -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) diff --git a/examples/utility_client/SConscript b/examples/utility_client/SConscript new file mode 100644 index 0000000000..7d4354ef8a --- /dev/null +++ b/examples/utility_client/SConscript @@ -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') diff --git a/examples/utility_client/utility_client.cpp b/examples/utility_client/utility_client.cpp new file mode 100644 index 0000000000..37e6149698 --- /dev/null +++ b/examples/utility_client/utility_client.cpp @@ -0,0 +1,63 @@ +#include + +#include + +#include + +typedef websocketpp::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; + } +}