Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
echo_server.cpp
Go to the documentation of this file.
2
4
5#include <iostream>
6
8
9using websocketpp::lib::placeholders::_1;
10using websocketpp::lib::placeholders::_2;
11using websocketpp::lib::bind;
12
13// pull out the type of messages sent by our config
15
16// Define a callback to handle incoming messages
18 std::cout << "on_message called with hdl: " << hdl.lock().get()
19 << " and message: " << msg->get_payload()
20 << std::endl;
21
22 // check for a special command to instruct the server to stop listening so
23 // it can be cleanly exited.
24 if (msg->get_payload() == "stop-listening") {
25 s->stop_listening();
26 return;
27 }
28
29 try {
30 s->send(hdl, msg->get_payload(), msg->get_opcode());
31 } catch (const websocketpp::lib::error_code& e) {
32 std::cout << "Echo failed because: " << e
33 << "(" << e.message() << ")" << std::endl;
34 }
35}
36
37int main() {
38 // Create a server endpoint
39 server echo_server;
40
41 try {
42 // Set logging settings
45
46 // Initialize Asio
47 echo_server.init_asio();
48
49 // Register our message handler
50 echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
51
52 // Listen on port 9002
53 echo_server.listen(9002);
54
55 // Start the server accept loop
56 echo_server.start_accept();
57
58 // Start the ASIO io_service run loop
59 echo_server.run();
60 } catch (websocketpp::exception const & e) {
61 std::cout << e.what() << std::endl;
62 } catch (...) {
63 std::cout << "other exception" << std::endl;
64 }
65}
void set_message_handler(message_handler h)
Definition endpoint.hpp:322
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition endpoint.hpp:231
void set_access_channels(log::level channels)
Set Access logging channel.
Definition endpoint.hpp:220
virtual char const * what() const
Definition error.hpp:263
Server endpoint role based on the given config.
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
int main()
websocketpp::server< websocketpp::config::asio > server
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
Definition levels.hpp:129
char * s