6 #include <websocketpp/config/asio_no_tls.hpp> 7 #include <websocketpp/server.hpp> 18 m_server.set_open_handler(bind(&count_server::on_open,
this,_1));
19 m_server.set_close_handler(bind(&count_server::on_close,
this,_1));
22 void on_open(connection_hdl hdl) {
23 std::lock_guard<std::mutex> lock(m_mutex);
24 m_connections.insert(hdl);
27 void on_close(connection_hdl hdl) {
28 std::lock_guard<std::mutex> lock(m_mutex);
29 m_connections.erase(hdl);
40 std::lock_guard<std::mutex> lock(m_mutex);
41 for (
auto it : m_connections) {
42 m_server.
send(it,ss.str(),websocketpp::frame::opcode::text);
47 void run(uint16_t port) {
53 typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
57 con_list m_connections;
63 std::thread t(std::bind(&count_server::count,&server));
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
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)