diff --git a/examples/iostream_server/iostream_server.cpp b/examples/iostream_server/iostream_server.cpp index bee4c1fb34..b1c3f0769f 100644 --- a/examples/iostream_server/iostream_server.cpp +++ b/examples/iostream_server/iostream_server.cpp @@ -5,8 +5,6 @@ #include #include -#include - typedef websocketpp::server server; using websocketpp::lib::placeholders::_1; @@ -18,15 +16,19 @@ 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; -*/ + if (msg->get_opcode() == websocketpp::frame::opcode::text) { + s->get_alog().write(websocketpp::log::alevel::app, + "Text Message Received: "+msg->get_payload()); + } else { + s->get_alog().write(websocketpp::log::alevel::app, + "Binary Message Received: "+websocketpp::utility::to_hex(msg->get_payload())); + } + 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;*/ + s->get_alog().write(websocketpp::log::alevel::app, + "Echo Failed: "+e.message()); } } @@ -35,14 +37,15 @@ int main() { std::ofstream log; try { - // Clear logging because we are using std out for data - // TODO: fix when we can log to files - s.set_error_channels(websocketpp::log::elevel::all); - s.set_access_channels(websocketpp::log::alevel::all); - + // set up access channels to only log interesting things + s.clear_access_channels(websocketpp::log::alevel::all); + s.set_access_channels(websocketpp::log::alevel::connect); + s.set_access_channels(websocketpp::log::alevel::disconnect); + s.set_access_channels(websocketpp::log::alevel::app); + // Log to a file rather than stdout, as we are using stdout for real + // output log.open("output.log"); - s.get_alog().set_ostream(&log); s.get_elog().set_ostream(&log); @@ -56,42 +59,27 @@ int main() { con->start(); - //std::cin >> *con; + // C++ iostream's don't support the idea of asynchronous i/o. As such + // there are two input strategies demonstrated here. Buffered I/O will + // read from stdin in chunks until EOF. This works very well for + // replaying canned connections as would be done in automated testing. + // + // If the server is being used live however, assuming input is being + // piped from elsewhere in realtime, this strategy will result in small + // messages being buffered forever. The non-buffered strategy below + // reads characters from stdin one at a time. This is inefficient and + // for more serious uses should be replaced with a platform specific + // asyncronous i/o technique like select, poll, IOCP, etc + bool buffered_io = false; - //log << "ready done" << std::endl; - - - char a; - while(std::cin.get(a)) { - con->readsome(&a,1); + if (buffered_io) { + std::cin >> *con; + } else { + char a; + while(std::cin.get(a)) { + con->readsome(&a,1); + } } - - /*char buf[512]; - size_t bytes_read; - size_t bytes_processed; - - while(std::cin) { - bytes_read = std::cin.readsome(buf,512); - - if (bytes_read > 0) { - std::cout << "read " << bytes_read << " bytes " - << websocketpp::utility::to_hex(buf,bytes_read) - << std::endl; - } - - bytes_processed = 0; - while (bytes_processed < bytes_read) { - std::cout << "foo" << std::endl; - bytes_processed += con->readsome(buf+bytes_processed, - bytes_read-bytes_processed); - std::cout << "bar" << std::endl; - sleep(1); - } - - sleep(1); - }*/ - std::cout << "end" << std::endl; - } catch (const std::exception & e) { std::cout << e.what() << std::endl; } catch (websocketpp::lib::error_code e) { @@ -100,26 +88,4 @@ int main() { std::cout << "other exception" << std::endl; } log.close(); -} - - - -/*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 +} \ No newline at end of file