Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
simple_broadcast_server.cpp
Go to the documentation of this file.
1#include <set>
2
5
7
9using websocketpp::lib::placeholders::_1;
10using websocketpp::lib::placeholders::_2;
11using websocketpp::lib::bind;
12
14public:
16 m_server.init_asio();
17
18 m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
19 m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
20 m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
21 }
22
24 m_connections.insert(hdl);
25 }
26
28 m_connections.erase(hdl);
29 }
30
32 for (auto it : m_connections) {
33 m_server.send(it,msg);
34 }
35 }
36
37 void run(uint16_t port) {
38 m_server.listen(port);
39 m_server.start_accept();
40 m_server.run();
41 }
42private:
43 typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
44
45 server m_server;
46 con_list m_connections;
47};
48
49int main() {
51 server.run(9002);
52}
void on_open(connection_hdl hdl)
void on_message(connection_hdl hdl, server::message_ptr msg)
void on_close(connection_hdl hdl)
void run(uint16_t port)
void set_message_handler(message_handler h)
Definition endpoint.hpp:322
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)
void set_open_handler(open_handler h)
Definition endpoint.hpp:277
void set_close_handler(close_handler h)
Definition endpoint.hpp:282
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::server< websocketpp::config::asio > server
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
unsigned short uint16_t
Definition stdint.h:125