@@ -47,8 +47,12 @@ struct action {
4747class broadcast_server {
4848public:
4949 broadcast_server () {
50+ websocketpp::lib::error_code ec;
5051 // Initialize Asio Transport
51- m_server.init_asio ();
52+ m_server.init_asio (ec);
53+ if (!ec) {
54+ return ;
55+ }
5256
5357 // Register handler callbacks
5458 m_server.set_open_handler (bind (&broadcast_server::on_open,this ,::_1));
@@ -57,18 +61,26 @@ class broadcast_server {
5761 }
5862
5963 void run (uint16_t port) {
64+ websocketpp::lib::error_code ec;
65+
6066 // listen on specified port
61- m_server.listen (port);
67+ m_server.listen (port,ec);
68+ if (!ec) {
69+ return ;
70+ }
6271
6372 // Start the server accept loop
64- m_server.start_accept ();
73+ m_server.start_accept (ec);
74+ if (!ec) {
75+ return ;
76+ }
6577
6678 // Start the ASIO io_service run loop
67- try {
79+ // try {
6880 m_server.run ();
69- } catch (const std::exception & e) {
70- std::cout << e.what () << std::endl;
71- }
81+ // } catch (const std::exception & e) {
82+ // std::cout << e.what() << std::endl;
83+ // }
7284 }
7385
7486 void on_open (connection_hdl hdl) {
@@ -100,6 +112,7 @@ class broadcast_server {
100112 }
101113
102114 void process_messages () {
115+ websocketpp::lib::error_code ec;
103116 while (1 ) {
104117 unique_lock<mutex> lock (m_action_lock);
105118
@@ -123,7 +136,10 @@ class broadcast_server {
123136
124137 con_list::iterator it;
125138 for (it = m_connections.begin (); it != m_connections.end (); ++it) {
126- m_server.send (*it,a.msg );
139+ m_server.send (*it,a.msg ,ec);
140+ if (ec) {
141+ return ;
142+ }
127143 }
128144 } else {
129145 // undefined.
@@ -143,7 +159,7 @@ class broadcast_server {
143159};
144160
145161int main () {
146- try {
162+ // try {
147163 broadcast_server server_instance;
148164
149165 // Start a thread to run the processing loop
@@ -154,7 +170,7 @@ int main() {
154170
155171 t.join ();
156172
157- } catch (websocketpp::exception const & e) {
158- std::cout << e.what () << std::endl;
159- }
173+ // } catch (websocketpp::exception const & e) {
174+ // std::cout << e.what() << std::endl;
175+ // }
160176}
0 commit comments