CWebsocketServer.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef CWebsocketServer_h
  2. #define CWebsocketServer_h
  3. #include "websocketpp/config/asio_no_tls.hpp"
  4. #include "websocketpp/config/asio.hpp"
  5. #include "websocketpp/server.hpp"
  6. #include "CMessage.h"
  7. #include "CWSCodec.h"
  8. #include "CSocketClient.h"
  9. #include "ISocketCallback.h"
  10. #include <boost/asio.hpp>
  11. #include <boost/thread/mutex.hpp>
  12. #include "baseEx.h"
  13. #include <functional>
  14. #include "EntitySessionManager.h"
  15. #include <memory>
  16. #include <vector>
  17. namespace Chromium{
  18. typedef websocketpp::server<websocketpp::config::asio> server;
  19. class CWebsocketServer : public ISocketCallback {
  20. public:
  21. CWebsocketServer(const char* strPath, CEntityBase* m_pEntity);
  22. void run();
  23. // socket callbacks
  24. void message_from_socket(CMessage& msg, unsigned int id);
  25. private:
  26. void message_handler(websocketpp::connection_hdl hdl, server::message_ptr msg);
  27. void deal_msg(std::string &payload, websocketpp::connection_hdl hdl);
  28. void open_handler(websocketpp::connection_hdl hdl);
  29. void close_handler(websocketpp::connection_hdl hdl);
  30. void do_run();
  31. void init_websocket();
  32. void init_entity_sessions();
  33. void do_sendJson(std::string js, int hdlID, unsigned int id);
  34. void do_sendJsonBroadcast(std::string js);
  35. std::string string_to_utf8(const std::string &str);
  36. void myTest();
  37. void updateMsgPool(std::string entityName, std::string &payload, websocketpp::connection_hdl hdl);
  38. void storeEntityWithCLass(std::string entityName, std::string entityClass);
  39. std::pair<bool, std::string> getEntityClass(std::string entityName);
  40. void do_sendJsonStartSession(std::string entityName, std::string entityClass);
  41. void do_relink();
  42. std::string restroreTransId(std::string payLoad);
  43. private:
  44. server m_wsserver;
  45. CWSCodec* m_serializer;
  46. CSocketClient* m_socket;
  47. EntitySessionManager* m_esm;
  48. boost::asio::io_service m_ios;
  49. std::map<unsigned int,websocketpp::connection_hdl> m_connection_hdls;
  50. std::map<std::string, std::vector<std::pair<websocketpp::connection_hdl, std::string>> > m_msg_pool;
  51. std::map<std::string, std::string> m_entityAndClass;
  52. //不知道为什么不能用map<string,map<>>,猜测应该是websocketpp::connection_hdl生成问题
  53. CEntityBase* m_pEntity;
  54. boost::mutex m_dealMsgLock;
  55. };
  56. }
  57. #endif