WebServer.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. #include <thread>
  6. #include <shared_mutex>
  7. #include <functional>
  8. #include <unordered_map>
  9. #include "rapidjson/rapidjson.h"
  10. #include "rapidjson/document.h"
  11. #include "rapidjson/istreamwrapper.h"
  12. #include "rapidjson/stringbuffer.h"
  13. #include "rapidjson/writer.h"
  14. #include <websocketpp/config/asio_no_tls.hpp>
  15. #include <websocketpp/server.hpp>
  16. #define Base_Uri_Length 128
  17. typedef websocketpp::server<websocketpp::config::asio> Server;
  18. using websocketpp::lib::placeholders::_1;
  19. using websocketpp::lib::placeholders::_2;
  20. using websocketpp::lib::bind;
  21. class CWebHandler;
  22. typedef websocketpp::config::asio::message_type::ptr message_ptr;
  23. struct KeyValue
  24. {
  25. std::string strKey;
  26. std::string strValue;
  27. };
  28. class CWebServer
  29. {
  30. public:
  31. typedef void (CWebServer::* FN)(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  32. CWebServer();
  33. ~CWebServer();
  34. static CWebServer& GetInstance();
  35. int Init(unsigned short usPort, const char* pBaseUri = "/ws");
  36. int Uninit();
  37. int StopWork();
  38. protected:
  39. int ThreadProccess();
  40. public:
  41. // bool validate(Server *s, websocketpp::connection_hdl hdl);
  42. // void on_http(Server* s, websocketpp::connection_hdl hdl);
  43. // void on_fail(Server* s, websocketpp::connection_hdl hdl);
  44. //连接打开回调函数
  45. //void on_open(Server* s, websocketpp::connection_hdl hdl);
  46. //连接关闭回调函数
  47. void on_close(Server* s, websocketpp::connection_hdl hdl);
  48. void on_message(Server* s, websocketpp::connection_hdl hdl, message_ptr msg);
  49. void Write(websocketpp::connection_hdl connect_hdl, const char* buffer);
  50. void Remove(websocketpp::connection_hdl connect_hdl);
  51. static int StringSplit(std::vector<std::string>& dst, const std::string& src, const std::string& separator);
  52. static std::string& StringTrim(std::string& str);
  53. static bool GetReqeustCommandAndParmeter(std::string strUri, std::string& strRequestOperateCommand, std::vector<KeyValue>& listRequestOperateParameter);
  54. int32_t GetId(websocketpp::connection_hdl hdl);
  55. private:
  56. void OnWebReqVideo(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  57. void OnWebRepVideo(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  58. void OnWebCancelReq(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  59. //void OnWebCloseVideo(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  60. void OnWebOffer(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  61. void OnWebAnswer(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  62. void OnWebCadidate(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  63. void OnWebSigin(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  64. void OnWebLeave(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  65. void OnWebHeartbeat(rapidjson::Document& value, websocketpp::connection_hdl hdl);
  66. protected:
  67. //unsigned short m_usPort;
  68. //char m_szBaseUri[Base_Uri_Length];
  69. Server m_server;
  70. std::thread websocket_thread;
  71. //bool m_bThreadExit;
  72. //std::list<websocketpp::connection_hdl> m_listClientConnection;
  73. std::unordered_map<std::string, FN> MapFn;
  74. std::unordered_map<Server::connection_ptr, std::shared_ptr<CWebHandler>> UserHandler;
  75. std::shared_mutex lock;
  76. };