Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
simple_count_server_thread.cpp
Go to the documentation of this file.
1#include <functional>
2#include <mutex>
3#include <set>
4#include <thread>
5
8
10
12
14public:
15 count_server() : m_count(0) {
16 m_server.init_asio();
17
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));
20 }
21
23 std::lock_guard<std::mutex> lock(m_mutex);
24 m_connections.insert(hdl);
25 }
26
28 std::lock_guard<std::mutex> lock(m_mutex);
29 m_connections.erase(hdl);
30 }
31
32 void count() {
33 while (1) {
34 sleep(1);
35 m_count++;
36
37 std::stringstream ss;
38 ss << m_count;
39
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);
43 }
44 }
45 }
46
47 void run(uint16_t port) {
48 m_server.listen(port);
49 m_server.start_accept();
50 m_server.run();
51 }
52private:
53 typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
54
55 int m_count;
56 server m_server;
57 con_list m_connections;
58 std::mutex m_mutex;
59};
60
61int main() {
63 std::thread t(std::bind(&count_server::count,&server));
64 server.run(9002);
65}
void on_open(connection_hdl hdl)
void on_close(connection_hdl hdl)
void run(uint16_t port)
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)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
websocketpp::server< websocketpp::config::asio > server
unsigned short uint16_t
Definition stdint.h:125
void lock()