linux版本中间件

WebSocketServer.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "WebSocketServer.h"
  2. #include "Log.h"
  3. #include <json/json.h>
  4. CWebSocketServer::CWebSocketServer()
  5. {
  6. }
  7. CWebSocketServer::~CWebSocketServer()
  8. {
  9. if (CConfig::GetInstance()->isWss()) {
  10. m_endpoint_tls.stop_perpetual();
  11. if (!m_endpoint_tls.stopped()) {
  12. m_endpoint_tls.stop();
  13. }
  14. }
  15. else {
  16. m_endpoint.stop_perpetual();
  17. if (!m_endpoint.stopped()) {
  18. m_endpoint.stop();
  19. }
  20. }
  21. }
  22. bool CWebSocketServer::init(uint16_t port)
  23. {
  24. m_port = port;
  25. try
  26. {
  27. if (CConfig::GetInstance()->isWss()) {
  28. printf("准备WSS\n");
  29. m_endpoint_tls.set_access_channels(websocketpp::log::alevel::none ^ websocketpp::log::alevel::none);
  30. m_endpoint_tls.set_message_handler(bind(&CWebSocketServer::on_message<server_tls>, this, &m_endpoint_tls, std::placeholders::_1, std::placeholders::_2));
  31. m_endpoint_tls.set_open_handler(bind(&CWebSocketServer::on_open<server_tls>, this, &m_endpoint_tls, std::placeholders::_1));
  32. m_endpoint_tls.set_close_handler(bind(&CWebSocketServer::on_close<server_tls>, this, &m_endpoint_tls, std::placeholders::_1));
  33. m_endpoint_tls.set_fail_handler(bind(&CWebSocketServer::on_fail<server_tls>, this, &m_endpoint_tls, std::placeholders::_1));
  34. m_endpoint_tls.set_tls_init_handler(bind(&CWebSocketServer::on_tls_init, this, std::placeholders::_1));
  35. m_endpoint_tls.init_asio();
  36. m_endpoint_tls.set_reuse_addr(true);
  37. m_endpoint_tls.start_perpetual();
  38. }
  39. else {
  40. printf("准备WS\n");
  41. m_endpoint.set_access_channels(websocketpp::log::alevel::none ^ websocketpp::log::alevel::none);
  42. m_endpoint.set_message_handler(bind(&CWebSocketServer::on_message<server>, this, &m_endpoint, std::placeholders::_1, std::placeholders::_2));
  43. m_endpoint.set_open_handler(bind(&CWebSocketServer::on_open<server>, this, &m_endpoint, std::placeholders::_1));
  44. m_endpoint.set_close_handler(bind(&CWebSocketServer::on_close<server>, this, &m_endpoint, std::placeholders::_1));
  45. m_endpoint.set_fail_handler(bind(&CWebSocketServer::on_fail<server>, this, &m_endpoint, std::placeholders::_1));
  46. m_endpoint.init_asio();
  47. m_endpoint.set_reuse_addr(true);
  48. m_endpoint.start_perpetual();
  49. }
  50. if (CConfig::GetInstance()->isWss()) {
  51. m_endpoint_tls.listen(m_port);
  52. m_endpoint_tls.start_accept();
  53. }
  54. else {
  55. m_endpoint.listen(m_port);
  56. m_endpoint.start_accept();
  57. }
  58. }
  59. catch (const std::exception& e)
  60. {
  61. LOG_ERROR_S(e.what());
  62. return false;
  63. }
  64. return true;
  65. }
  66. bool CWebSocketServer::sendMsg(websocketpp::connection_hdl hdl, string msg)
  67. {
  68. try
  69. {
  70. websocketpp::frame::opcode::value op = websocketpp::frame::opcode::value::TEXT;
  71. if (CConfig::GetInstance()->isWss()) {
  72. m_endpoint_tls.send(hdl, msg, op);
  73. }
  74. else {
  75. m_endpoint.send(hdl, msg, op);
  76. }
  77. LOG_INFO("消息发送到坐席成功,[%s]",msg.c_str());
  78. }
  79. catch (websocketpp::exception const & e) {
  80. stringstream str;
  81. str << "Echo failed because: "
  82. << "(" << e.what() << ")";
  83. LOG_ERROR_S(str.str());
  84. LOG_ERROR("消息发送到坐席失败,[%s]", msg.c_str());
  85. return false;
  86. }
  87. return true;
  88. }
  89. void CWebSocketServer::run()
  90. {
  91. // Start the Asio io_service run loop
  92. if (CConfig::GetInstance()->isWss()) {
  93. m_endpoint_tls.run();
  94. }
  95. else {
  96. m_endpoint.run();
  97. }
  98. }
  99. void CWebSocketServer::close(websocketpp::connection_hdl hdl)
  100. {
  101. if (CConfig::GetInstance()->isWss()) {
  102. m_endpoint_tls.close(hdl, websocketpp::close::status::normal, "Normal");
  103. }
  104. else {
  105. m_endpoint.close(hdl, websocketpp::close::status::normal, "Normal");
  106. }
  107. }
  108. void CWebSocketServer::stop()
  109. {
  110. if (CConfig::GetInstance()->isWss()) {
  111. if (!m_endpoint_tls.stopped()) {
  112. m_endpoint_tls.stop();
  113. }
  114. }
  115. else {
  116. if (!m_endpoint.stopped()) {
  117. m_endpoint.stop();
  118. }
  119. }
  120. }
  121. // Define a callback to handle incoming messages
  122. template <typename EndpointType>
  123. void CWebSocketServer::on_message(EndpointType* s, websocketpp::connection_hdl hdl, typename EndpointType::message_ptr msg)
  124. {
  125. string msgInfo;
  126. msgInfo = msg->get_payload();
  127. stringstream str;
  128. str << "on_message called with hdl: " << hdl.lock().get()
  129. << " and message: " <<
  130. msgInfo << s->get_con_from_hdl(hdl)->get_remote_endpoint();
  131. LOG_DEBUG_S(str.str());
  132. if (this->pRecvFun != nullptr)
  133. this->pRecvFun(hdl, msgInfo); // 收到消息回调函数
  134. }
  135. template <typename EndpointType>
  136. void CWebSocketServer::on_open(EndpointType* s, websocketpp::connection_hdl hdl) {
  137. stringstream str;
  138. str << "on_open called with hdl: " << hdl.lock().get()
  139. << s->get_con_from_hdl(hdl)->get_remote_endpoint();
  140. LOG_DEBUG_S(str.str());
  141. }
  142. template <typename EndpointType>
  143. void CWebSocketServer::on_close(EndpointType* s, websocketpp::connection_hdl hdl) {
  144. stringstream str;
  145. str << "on_close called with hdl: " << hdl.lock().get()
  146. << s->get_con_from_hdl(hdl)->get_remote_endpoint();
  147. LOG_DEBUG_S(str.str());
  148. if (this->pCloseFun != nullptr)
  149. this->pCloseFun(hdl); // 连接断开回调函数
  150. }
  151. template <typename EndpointType>
  152. void CWebSocketServer::on_fail(EndpointType* s, websocketpp::connection_hdl hdl)
  153. {
  154. stringstream str;
  155. str << "on_fail called with hdl: " << hdl.lock().get()
  156. << s->get_con_from_hdl(hdl)->get_remote_endpoint();
  157. LOG_DEBUG_S(str.str());
  158. }