diff --git a/SConstruct b/SConstruct index 72d8c44fe8..45ea5b6194 100644 --- a/SConstruct +++ b/SConstruct @@ -190,6 +190,10 @@ echo_server_tls = SConscript('#/examples/echo_server_tls/SConscript',variant_dir # broadcast_server broadcast_server = SConscript('#/examples/broadcast_server/SConscript',variant_dir = builddir + 'broadcast_server',duplicate = 0) + +# echo_client +echo_client = SConscript('#/examples/echo_client/SConscript',variant_dir = builddir + 'echo_client',duplicate = 0) + # #wsperf = SConscript('#/examples/wsperf/SConscript', # variant_dir = builddir + 'wsperf', diff --git a/examples/echo_client/SConscript b/examples/echo_client/SConscript new file mode 100644 index 0000000000..291ade5df0 --- /dev/null +++ b/examples/echo_client/SConscript @@ -0,0 +1,23 @@ +## Main development 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('echo_client', ["echo_client.cpp"], LIBS = ALL_LIBS) +else: + ALL_LIBS = boostlibs(['system','regex','random'],env) + [platform_libs] + [polyfill_libs] + prgs += env.Program('echo_client', ["echo_client.cpp"], LIBS = ALL_LIBS) + +Return('prgs') diff --git a/examples/echo_client/echo_client.cpp b/examples/echo_client/echo_client.cpp new file mode 100644 index 0000000000..a7265352d0 --- /dev/null +++ b/examples/echo_client/echo_client.cpp @@ -0,0 +1,70 @@ +#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; + + +// Define a callback to handle incoming messages +void on_open(client* c, websocketpp::connection_hdl hdl) { + std::cout << "on_open called for " << hdl.lock().get() << std::endl; + try { + c->send(hdl, "Foo", websocketpp::frame::opcode::text); + } catch (const websocketpp::lib::error_code& e) { + std::cout << "Send failed because: " << e + << "(" << e.message() << ")" << std::endl; + } +} + +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; + + /*try { + c->send(hdl, msg->get_payload(), msg->get_opcode()); + } catch (const websocketpp::lib::error_code& e) { + std::cout << "Echo failed because: " << e + << "(" << e.message() << ")" << std::endl; + }*/ +} + +int main() { + // Create a server endpoint + client echo_client; + + try { + // Set logging settings + echo_client.set_access_channels(websocketpp::log::alevel::all); + echo_client.clear_access_channels(websocketpp::log::alevel::frame_payload); + + // Initialize ASIO + echo_client.init_asio(); + + // Register our handlers + echo_client.set_message_handler(bind(&on_message,&echo_client,::_1,::_2)); + echo_client.set_open_handler(bind(&on_open,&echo_client,::_1)); + + websocketpp::lib::error_code ec; + client::connection_ptr con = echo_client.get_connection("ws://localhost:9002/", ec); + echo_client.connect(con); + + // Start the ASIO io_service run loop + echo_client.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; + } +}