From 07a4394ba68ff90860ba0c8666cd268707cd39ee Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Tue, 23 Apr 2013 09:26:57 -0500 Subject: [PATCH] new iostream example --- SConstruct | 3 + examples/iostream_server/SConscript | 23 +++++ examples/iostream_server/iostream_server.cpp | 89 ++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 examples/iostream_server/SConscript create mode 100644 examples/iostream_server/iostream_server.cpp diff --git a/SConstruct b/SConstruct index ec0d956a2b..323963b23e 100644 --- a/SConstruct +++ b/SConstruct @@ -197,6 +197,9 @@ echo_client = SConscript('#/examples/echo_client/SConscript',variant_dir = build # subprotocol_server subprotocol_server = SConscript('#/examples/subprotocol_server/SConscript',variant_dir = builddir + 'subprotocol_server',duplicate = 0) +# iostream_server +iostream_server = SConscript('#/examples/iostream_server/SConscript',variant_dir = builddir + 'iostream_server',duplicate = 0) + # #wsperf = SConscript('#/examples/wsperf/SConscript', # variant_dir = builddir + 'wsperf', diff --git a/examples/iostream_server/SConscript b/examples/iostream_server/SConscript new file mode 100644 index 0000000000..3ea3f68e3f --- /dev/null +++ b/examples/iostream_server/SConscript @@ -0,0 +1,23 @@ +## iostream server 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('iostream_server', ["iostream_server.cpp"], LIBS = ALL_LIBS) +else: + ALL_LIBS = boostlibs(['system','regex'],env) + [platform_libs] + [polyfill_libs] + prgs += env.Program('iostream_server', ["iostream_server.cpp"], LIBS = ALL_LIBS) + +Return('prgs') diff --git a/examples/iostream_server/iostream_server.cpp b/examples/iostream_server/iostream_server.cpp new file mode 100644 index 0000000000..b3cc8d3f66 --- /dev/null +++ b/examples/iostream_server/iostream_server.cpp @@ -0,0 +1,89 @@ +#include + +#include + +#include + +typedef websocketpp::server server; + +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 server::message_ptr message_ptr; + +// Define a callback to handle incoming messages +void on_message(server* s, 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 { + s->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() { + server s; + + try { + // Clear logging because we are using std out for data + // TODO: fix when we can log to files + s.clear_error_channels(websocketpp::log::elevel::all); + s.clear_access_channels(websocketpp::log::alevel::all); + + // print all output to stdout + s.register_ostream(&std::cout); + + // Register our message handler + s.set_message_handler(bind(&on_message,&s,::_1,::_2)); + + server::connection_ptr con = s.get_connection(); + + con->start(); + + std::cin >> *con; + + std::cout << "ready done" << std::endl; + + /*char buf[512]; + size_t bytes_read; + while(std::cin) { + bytes_read = std::cin.readsome(buf,512); + + + }*/ + } 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; + } +} + + + +/*server test_server; + server::connection_ptr con; + + test_server.set_message_handler(bind(&echo_func,&test_server,::_1,::_2)); + + std::stringstream output; + + test_server.register_ostream(&output); + + con = test_server.get_connection(); + + con->start(); + + std::stringstream channel; + + channel << input; + channel >> *con; + + return output.str();*/ \ No newline at end of file