fork(27) download
  1. #include <websocketpp/config/asio_no_tls.hpp>
  2.  
  3. #include <websocketpp/server.hpp>
  4. #include "../../nlohmann/json.hpp"
  5.  
  6. #include <iostream>
  7. #include <set>
  8. #include <map>
  9.  
  10. /*#include <boost/thread.hpp>
  11. #include <boost/thread/mutex.hpp>
  12. #include <boost/thread/condition_variable.hpp>*/
  13. #include <websocketpp/common/thread.hpp>
  14. using json = nlohmann::json;
  15.  
  16. typedef websocketpp::server<websocketpp::config::asio> server;
  17.  
  18. using websocketpp::connection_hdl;
  19. using websocketpp::lib::placeholders::_1;
  20. using websocketpp::lib::placeholders::_2;
  21. using websocketpp::lib::bind;
  22.  
  23. using websocketpp::lib::thread;
  24. using websocketpp::lib::mutex;
  25. using websocketpp::lib::lock_guard;
  26. using websocketpp::lib::unique_lock;
  27. using websocketpp::lib::condition_variable;
  28.  
  29. /* on_open insert connection_hdl into channel
  30.  * on_close remove connection_hdl from channel
  31.  * on_message queue send to all channels
  32.  */
  33.  
  34. enum action_type {
  35. SUBSCRIBE,
  36. UNSUBSCRIBE,
  37. MESSAGE
  38. };
  39.  
  40. struct action {
  41. action(action_type t, connection_hdl h) : type(t), hdl(h) {}
  42. action(action_type t, connection_hdl h, server::message_ptr m)
  43. : type(t), hdl(h), msg(m) {}
  44.  
  45. action_type type;
  46. websocketpp::connection_hdl hdl;
  47. server::message_ptr msg;
  48. };
  49.  
  50. class broadcast_server {
  51. public:
  52. broadcast_server() {
  53. // Initialize Asio Transport
  54. m_server.init_asio();
  55.  
  56. // Register handler callbacks
  57. m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
  58. m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
  59. m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
  60. }
  61.  
  62. void run(uint16_t port) {
  63. // listen on specified port
  64. m_server.listen(port);
  65.  
  66. // Start the server accept loop
  67. m_server.start_accept();
  68.  
  69. // Start the ASIO io_service run loop
  70. try {
  71. m_server.run();
  72. } catch (const std::exception & e) {
  73. std::cout << e.what() << std::endl;
  74. }
  75. }
  76.  
  77. void on_open(connection_hdl hdl) {
  78. {
  79. lock_guard<mutex> guard(m_action_lock);
  80. //std::cout << "on_open" << std::endl;
  81. m_actions.push(action(SUBSCRIBE,hdl));
  82. }
  83. m_action_cond.notify_one();
  84. }
  85.  
  86. void on_close(connection_hdl hdl) {
  87. {
  88. lock_guard<mutex> guard(m_action_lock);
  89. //std::cout << "on_close" << std::endl;
  90. m_actions.push(action(UNSUBSCRIBE,hdl));
  91. }
  92. m_action_cond.notify_one();
  93. }
  94.  
  95. void on_message(connection_hdl hdl, server::message_ptr msg) {
  96. // queue message up for sending by processing thread
  97. {
  98. lock_guard<mutex> guard(m_action_lock);
  99. //std::cout << "on_message" << std::endl;
  100. m_actions.push(action(MESSAGE,hdl,msg));
  101. }
  102. m_action_cond.notify_one();
  103. }
  104.  
  105. void process_messages() {
  106. while(1) {
  107. unique_lock<mutex> lock(m_action_lock);
  108.  
  109. while(m_actions.empty()) {
  110. m_action_cond.wait(lock);
  111. }
  112.  
  113. action a = m_actions.front();
  114. m_actions.pop();
  115.  
  116. lock.unlock();
  117.  
  118. if (a.type == SUBSCRIBE) {
  119. lock_guard<mutex> guard(m_connection_lock);
  120. //m_connections.insert(a.hdl);
  121.  
  122. m_NumOfClient = m_NumOfClient + 1; // clean
  123. if (m_NumOfClient > 2) { // 若房間大於2人
  124. m_iKey = m_iKey + 1; // 開新房
  125. m_NumOfClient = 0;
  126. }
  127. m_HubTo_list[m_iKey].insert(a.hdl);
  128.  
  129. } else if (a.type == UNSUBSCRIBE) {
  130. lock_guard<mutex> guard(m_connection_lock);
  131. m_connections.erase(a.hdl);
  132. } else if (a.type == MESSAGE) {
  133. lock_guard<mutex> guard(m_connection_lock);
  134. m_person["neame"] = "NULL";
  135. m_person["age"] = 20;
  136. //con_list::iterator it;
  137. int index = 0;
  138. auto folder = a.msg->get_payload();
  139. std::cout << "folder ==" << folder << std::endl;
  140. //folder.erase(std::remove(folder.begin(), folder.end(), '\n'), folder.end());
  141. //folder.erase(std::remove(folder.begin(), folder.end(), '\t'), folder.end());
  142. //folder.erase(std::remove(folder.begin(), folder.end(), ' '), folder.end());
  143. std::replace(folder.begin(), folder.end(), '/n', '//n');
  144. //auto tt = websocketpp::frame::opcode::TEXT;
  145. json j_from_cbor = folder;
  146. //std::cin >> j_from_cbor;
  147. //auto keep = it.value("_id", 0);
  148. //std::cout << "kepp = " << keep << std::endl;
  149. //auto keep["_id"] = j_from_cbor["_id"];
  150. for (json::iterator it = j_from_cbor.begin(); it != j_from_cbor.end(); ++it) {
  151. std::cout << it.key() << " : " << it.value() << "\n";
  152. }
  153.  
  154. try {
  155. //json::parse(it);
  156. }
  157. catch (json::parse_error e) {
  158. // output exception information
  159. std::cout << "message: " << e.what() << '\n'
  160. << "exception id: " << e.id << '\n'
  161. << "byte position of error: " << e.byte << std::endl;
  162.  
  163. }
  164. std::cout << "---------------------START--------------------------" << std::endl;
  165. std::cout << j_from_cbor << std::endl;
  166. std::cout << "---------------------END--------FFOLDER--------------------------" << std::endl;
  167.  
  168.  
  169.  
  170. con_list set = m_HubTo_list.at(index);
  171. for (con_list::iterator getit = set.begin(); getit != set.end(); ++getit) {
  172. m_server.send(*getit, a.msg);
  173. }
  174.  
  175. } else {
  176. // undefined.
  177. }
  178. }
  179. }
  180. private:
  181. typedef std::set<connection_hdl,std::owner_less<connection_hdl> > con_list;
  182. typedef std::map<int , con_list> HubToCon_list; // int == NameOfChatroom , con_list == list of client in this room
  183.  
  184. server m_server;
  185. con_list m_connections;
  186. //
  187. HubToCon_list m_HubTo_list ; // 建立一個多通道(房) 聊天
  188. int m_iKey = 0;
  189. int m_NumOfClient = 0;
  190. //
  191. std::queue<action> m_actions;
  192.  
  193. mutex m_action_lock;
  194. mutex m_connection_lock;
  195. condition_variable m_action_cond;
  196. json m_person;
  197. };
  198.  
  199. int main() {
  200. try {
  201. broadcast_server server_instance;
  202.  
  203. // Start a thread to run the processing loop
  204. thread t(bind(&broadcast_server::process_messages,&server_instance));
  205.  
  206. // Run the asio loop with the main thread
  207. server_instance.run(9002);
  208.  
  209. t.join();
  210.  
  211. } catch (websocketpp::exception const & e) {
  212. std::cout << e.what() << std::endl;
  213. }
  214. }
  215.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:46: fatal error: websocketpp/config/asio_no_tls.hpp: No such file or directory
 #include <websocketpp/config/asio_no_tls.hpp>
                                              ^
compilation terminated.
stdout
Standard output is empty