mirror of
https://github.com/XRPLF/rippled.git
synced 2026-04-29 15:37:57 +00:00
new iostream example
This commit is contained in:
@@ -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',
|
||||
|
||||
23
examples/iostream_server/SConscript
Normal file
23
examples/iostream_server/SConscript
Normal file
@@ -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')
|
||||
89
examples/iostream_server/iostream_server.cpp
Normal file
89
examples/iostream_server/iostream_server.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include <websocketpp/config/core.hpp>
|
||||
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
typedef websocketpp::server<websocketpp::config::core> 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();*/
|
||||
Reference in New Issue
Block a user