Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
iostream_server.cpp
Go to the documentation of this file.
2
4
5#include <iostream>
6#include <fstream>
7
9
10using websocketpp::lib::placeholders::_1;
11using websocketpp::lib::placeholders::_2;
12using websocketpp::lib::bind;
13
14// pull out the type of messages sent by our config
16
17// Define a callback to handle incoming messages
19 if (msg->get_opcode() == websocketpp::frame::opcode::text) {
20 s->get_alog().write(websocketpp::log::alevel::app,
21 "Text Message Received: "+msg->get_payload());
22 } else {
23 s->get_alog().write(websocketpp::log::alevel::app,
24 "Binary Message Received: "+websocketpp::utility::to_hex(msg->get_payload()));
25 }
26
27 try {
28 s->send(hdl, msg->get_payload(), msg->get_opcode());
29 } catch (const websocketpp::lib::error_code& e) {
30 s->get_alog().write(websocketpp::log::alevel::app,
31 "Echo Failed: "+e.message());
32 }
33}
34
35int main() {
36 server s;
37 std::ofstream log;
38
39 try {
40 // set up access channels to only log interesting things
41 s.clear_access_channels(websocketpp::log::alevel::all);
42 s.set_access_channels(websocketpp::log::alevel::connect);
43 s.set_access_channels(websocketpp::log::alevel::disconnect);
44 s.set_access_channels(websocketpp::log::alevel::app);
45
46 // Log to a file rather than stdout, as we are using stdout for real
47 // output
48 log.open("output.log");
49 s.get_alog().set_ostream(&log);
50 s.get_elog().set_ostream(&log);
51
52 // print all output to stdout
53 s.register_ostream(&std::cout);
54
55 // Register our message handler
56 s.set_message_handler(bind(&on_message,&s,::_1,::_2));
57
58 server::connection_ptr con = s.get_connection();
59
60 con->start();
61
62 // C++ iostream's don't support the idea of asynchronous i/o. As such
63 // there are two input strategies demonstrated here. Buffered I/O will
64 // read from stdin in chunks until EOF. This works very well for
65 // replaying canned connections as would be done in automated testing.
66 //
67 // If the server is being used live however, assuming input is being
68 // piped from elsewhere in realtime, this strategy will result in small
69 // messages being buffered forever. The non-buffered strategy below
70 // reads characters from stdin one at a time. This is inefficient and
71 // for more serious uses should be replaced with a platform specific
72 // asyncronous i/o technique like select, poll, IOCP, etc
73 bool buffered_io = false;
74
75 if (buffered_io) {
76 std::cin >> *con;
77 con->eof();
78 } else {
79 char a;
80 while(std::cin.get(a)) {
81 con->read_some(&a,1);
82 }
83 con->eof();
84 }
85 } catch (websocketpp::exception const & e) {
86 std::cout << e.what() << std::endl;
87 }
88 log.close();
89}
void close(connection_hdl hdl, close::status::value const code, std::string const &reason, lib::error_code &ec)
virtual char const * what() const
Definition error.hpp:263
Server endpoint role based on the given config.
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
websocketpp::server< websocketpp::config::core > server
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
int main()
std::string to_hex(std::string const &input)
Convert std::string to ascii printed string of hex digits.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
static level const connect
Information about new connections.
Definition levels.hpp:121
static level const app
Special channel for application specific logs. Not used by the library.
Definition levels.hpp:143
static level const disconnect
One line for each closed connection. Includes closing codes and reasons.
Definition levels.hpp:123
char * s