1 #include <websocketpp/config/asio_no_tls_client.hpp>
2 #include <websocketpp/client.hpp>
4 // This header pulls in the WebSocket++ abstracted thread support that will
5 // select between boost::thread and std::thread based on how the build system
7 #include <websocketpp/common/thread.hpp>
10 * The telemetry client connects to a WebSocket server and sends a message every
11 * second containing an integer count. This example can be used as the basis for
12 * programs where a client connects and pushes data for logging, stress/load
15 class telemetry_client {
17 typedef websocketpp::client<websocketpp::config::asio_client> client;
18 typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
20 telemetry_client() : m_open(false),m_done(false) {
21 // set up access channels to only log interesting things
22 m_client.clear_access_channels(websocketpp::log::alevel::all);
23 m_client.set_access_channels(websocketpp::log::alevel::connect);
24 m_client.set_access_channels(websocketpp::log::alevel::disconnect);
25 m_client.set_access_channels(websocketpp::log::alevel::app);
27 // Initialize the Asio transport policy
30 // Bind the handlers we are using
31 using websocketpp::lib::placeholders::_1;
32 using websocketpp::lib::bind;
33 m_client.set_open_handler(bind(&telemetry_client::on_open,this,_1));
34 m_client.set_close_handler(bind(&telemetry_client::on_close,this,_1));
35 m_client.set_fail_handler(bind(&telemetry_client::on_fail,this,_1));
38 // This method will block until the connection is complete
39 void run(const std::string & uri) {
40 // Create a new connection to the given URI
41 websocketpp::lib::error_code ec;
42 client::connection_ptr con = m_client.get_connection(uri, ec);
44 m_client.get_alog().write(websocketpp::log::alevel::app,
45 "Get Connection Error: "+ec.message());
49 // Grab a handle for this connection so we can talk to it in a thread
50 // safe manor after the event loop starts.
51 m_hdl = con->get_handle();
53 // Queue the connection. No DNS queries or network connections will be
54 // made until the io_service event loop is run.
55 m_client.connect(con);
57 // Create a thread to run the ASIO io_service event loop
58 websocketpp::lib::thread asio_thread(&client::run, &m_client);
60 // Create a thread to run the telemetry loop
61 websocketpp::lib::thread telemetry_thread(&telemetry_client::telemetry_loop,this);
64 telemetry_thread.join();
67 // The open handler will signal that we are ready to start sending telemetry
68 void on_open(websocketpp::connection_hdl) {
69 m_client.get_alog().write(websocketpp::log::alevel::app,
70 "Connection opened, starting telemetry!");
72 scoped_lock guard(m_lock);
76 // The close handler will signal that we should stop sending telemetry
77 void on_close(websocketpp::connection_hdl) {
78 m_client.get_alog().write(websocketpp::log::alevel::app,
79 "Connection closed, stopping telemetry!");
81 scoped_lock guard(m_lock);
85 // The fail handler will signal that we should stop sending telemetry
86 void on_fail(websocketpp::connection_hdl) {
87 m_client.get_alog().write(websocketpp::log::alevel::app,
88 "Connection failed, stopping telemetry!");
90 scoped_lock guard(m_lock);
94 void telemetry_loop() {
96 std::stringstream val;
97 websocketpp::lib::error_code ec;
103 scoped_lock guard(m_lock);
104 // If the connection has been closed, stop generating telemetry
107 // If the connection hasn't been opened yet wait a bit and retry
119 val << "count is " << count++;
121 m_client.get_alog().write(websocketpp::log::alevel::app, val.str());
122 m_client.send(m_hdl,val.str(),websocketpp::frame::opcode::text,ec);
124 // The most likely error that we will get is that the connection is
125 // not in the right state. Usually this means we tried to send a
126 // message to a connection that was closed or in the process of
127 // closing. While many errors here can be easily recovered from,
128 // in this simple example, we'll stop the telemetry loop.
130 m_client.get_alog().write(websocketpp::log::alevel::app,
131 "Send Error: "+ec.message());
140 websocketpp::connection_hdl m_hdl;
141 websocketpp::lib::mutex m_lock;
146 int main(int argc, char* argv[]) {
149 std::string uri = "ws://localhost:9002";