46 using websocketpp::lib::placeholders::_1;
47 using websocketpp::lib::bind;
53 void run(std::string docroot, uint16_t port) {
55 ss <<
"Running telemetry server on port "<< port <<
" using docroot=" << docroot;
73 std::cout << e.
what() << std::endl;
80 websocketpp::lib::bind(
83 websocketpp::lib::placeholders::_1
88 void on_timer(websocketpp::lib::error_code
const & ec) {
92 "Timer Error: "+ec.message());
96 std::stringstream val;
97 val <<
"count is " << m_count++;
101 for (it = m_connections.begin(); it != m_connections.end(); ++it) {
114 std::string filename = con->get_resource();
115 std::string response;
118 "http request1: "+filename);
120 if (filename ==
"/") {
121 filename = m_docroot+
"index.html";
123 filename = m_docroot+filename.substr(1);
127 "http request2: "+filename);
129 file.open(filename.c_str(), std::ios::in);
132 std::stringstream ss;
134 ss <<
"<!doctype html><html><head>" 135 <<
"<title>Error 404 (Resource not found)</title><body>" 136 <<
"<h1>Error 404</h1>" 137 <<
"<p>The requested URL " << filename <<
" was not found on this server.</p>" 138 <<
"</body></head></html>";
140 con->set_body(ss.str());
145 file.seekg(0, std::ios::end);
146 response.reserve(file.tellg());
147 file.seekg(0, std::ios::beg);
149 response.assign((std::istreambuf_iterator<char>(file)),
150 std::istreambuf_iterator<char>());
152 con->set_body(response);
157 m_connections.insert(hdl);
161 m_connections.erase(hdl);
164 typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
167 con_list m_connections;
170 std::string m_docroot;
176 int main(
int argc,
char* argv[]) {
180 uint16_t port = 9002;
183 std::cout <<
"Usage: telemetry_server [documentroot] [port]" << std::endl;
188 docroot = std::string(argv[1]);
192 int i = atoi(argv[2]);
193 if (i <= 0 || i > 65535) {
194 std::cout <<
"invalid port" << std::endl;
201 s.
run(docroot, port);
static level const all
Special aggregate value representing "all levels".
void write(level, std::string const &)
Write a string message to the given channel.
void set_open_handler(open_handler h)
static level const app
Special channel for application specific logs. Not used by the library.
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
void on_close(connection_hdl hdl)
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of timer handle.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
alog_type & get_alog()
Get reference to access logger.
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
void set_close_handler(close_handler h)
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
virtual char const * what() const
Table::const_iterator iterator
void on_open(connection_hdl hdl)
websocketpp::server< websocketpp::config::asio > server
int main(int argc, char *argv[])
void on_http(connection_hdl hdl)
The telemetry server accepts connections and sends a message every second to each client containing a...
static level const access_core
Aggregate package representing the commonly used core access channels Connect, Disconnect, Fail, and HTTP.
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
void set_access_channels(log::level channels)
Set Access logging channel.
void run(std::string docroot, uint16_t port)
void set_http_handler(http_handler h)
void on_timer(websocketpp::lib::error_code const &ec)
void clear_access_channels(log::level channels)
Clear Access logging channels.
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
std::size_t run()
wraps the run method of the internal io_service object
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
void send(connection_hdl hdl, std::string const &payload, frame::opcode::value op, lib::error_code &ec)
Create a message and add it to the outgoing send queue (exception free)
websocketpp::connection_hdl connection_hdl