linux版本中间件

WebSocketServer.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include <websocketpp/config/asio.hpp>
  3. #include <websocketpp/server.hpp>
  4. #include <functional>
  5. #include <iostream>
  6. #include <string>
  7. #include <unordered_map>
  8. #include <mutex>
  9. #include <regex>
  10. #include <json/json.h>
  11. #include "Config.h"
  12. using namespace std;
  13. class CWebSocketServer
  14. {
  15. private:
  16. typedef websocketpp::server<websocketpp::config::asio> server;
  17. typedef websocketpp::server<websocketpp::config::asio_tls> server_tls;
  18. typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
  19. public:
  20. CWebSocketServer();
  21. ~CWebSocketServer();
  22. bool init(uint16_t port = 9002);
  23. bool sendMsg(websocketpp::connection_hdl hdl, string msg);
  24. void run();
  25. void close(websocketpp::connection_hdl hdl); // 关闭与指定客户端的连接
  26. void stop();
  27. typedef function<void(websocketpp::connection_hdl hdl, string msg)> Callback_RecvMsg;
  28. typedef function<void(websocketpp::connection_hdl hdl)> Callback_Close;
  29. void setCallbackRecvMsg(Callback_RecvMsg pRecvFun) { this->pRecvFun = pRecvFun; };
  30. void setCallbackClose(Callback_Close pCloseFun) { this->pCloseFun = pCloseFun; };
  31. private:
  32. template <typename EndpointType>
  33. void on_message(EndpointType* s, websocketpp::connection_hdl hdl, typename EndpointType::message_ptr msg);
  34. template <typename EndpointType>
  35. void on_open(EndpointType* s, websocketpp::connection_hdl hdl) ;
  36. template <typename EndpointType>
  37. void on_close(EndpointType* s, websocketpp::connection_hdl hdl);
  38. template <typename EndpointType>
  39. void on_fail(EndpointType* s, websocketpp::connection_hdl hdl);
  40. context_ptr on_tls_init(websocketpp::connection_hdl hdl) {
  41. std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
  42. context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv13_server);
  43. try {
  44. ctx->set_options(boost::asio::ssl::context::default_workarounds |
  45. boost::asio::ssl::context::no_sslv2 |
  46. boost::asio::ssl::context::no_sslv3 |
  47. boost::asio::ssl::context::no_tlsv1 |
  48. boost::asio::ssl::context::single_dh_use);
  49. ctx->use_certificate_file("server.crt", boost::asio::ssl::context::pem); // "server.crt"
  50. ctx->use_private_key_file("server.crt", boost::asio::ssl::context::pem); // "server.key"
  51. }
  52. catch (std::exception& e) {
  53. char str[1024];
  54. snprintf(str, 1024, "[%s :%d][ %s]", __FUNCTION__, __LINE__, e.what());
  55. std::cout << str << std::endl;
  56. }
  57. return ctx;
  58. }
  59. private:
  60. server m_endpoint;
  61. server_tls m_endpoint_tls;
  62. uint16_t m_port; // 端口
  63. Callback_RecvMsg pRecvFun; // 接收消息回调函数
  64. Callback_Close pCloseFun; // 连接断开回调函数
  65. };