wsClient.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @brief
  3. websocket客户端类
  4. * @version
  5. V 1.0.0
  6. * @author
  7. 王益俊
  8. * @date
  9. 创建时间: 2018-08-14\n
  10. * @note
  11. 2018-08-14 创建类。\n
  12. * @warning
  13. * @bug
  14. */
  15. #pragma once
  16. #include <string>
  17. #include <mutex>
  18. #include <condition_variable>
  19. #include <map>
  20. #include <rapidjson/writer.h>
  21. #include <rapidjson/stringbuffer.h>
  22. #include <rapidjson/prettywriter.h>
  23. #include <boost/function.hpp>
  24. #include <boost/bind.hpp>
  25. #include "sio_client.h"
  26. #include "jsonBuilder.h"
  27. namespace YA
  28. {
  29. //消息处理函数类型
  30. typedef boost::function<void( int, std::string const&, sio::message::ptr const&, bool, sio::message::list & )> MSG_HANDLE_FUNC_TYPE;
  31. class wsClient
  32. {
  33. private:
  34. sio::client __wsclient;//websocket客户端
  35. int __ID;//唯一标识
  36. std::string __uri;//连接字符串
  37. bool __Connected;//是否连接上服务器端
  38. bool __SktOpened;//Socket是否已打开
  39. bool __Logined;//是否已登录成功
  40. std::condition_variable_any __cond;//条件变量
  41. std::mutex __lock;//锁(配合__cond)
  42. std::string __LastError;//最新错误
  43. std::map<std::string, MSG_HANDLE_FUNC_TYPE> __MsgFuncList;//消息处理函数列表(key是命令,value是处理函数)
  44. std::recursive_mutex __send_lock;//发送锁
  45. jsonBuilder __jsBuilder;//json构造器
  46. unsigned int __connet_time; //连接时间
  47. unsigned int __recv_ping_time; //接受ping消息的时间
  48. protected:
  49. /**
  50. * @brief 重置状态函数。
  51. */
  52. void _reset();
  53. /**
  54. * @brief 连接成功回调函数。
  55. */
  56. void _on_connected();
  57. /**
  58. * @brief 连接关闭回调函数。
  59. * @param [in] sio::client::close_reason const& reason 关闭的原因\n
  60. */
  61. void _on_close( sio::client::close_reason const& reason );
  62. /**
  63. * @brief 重连回调函数。
  64. * @param [in] unsigned p1 ???\n
  65. * @param [in] unsigned p2 ???\n
  66. */
  67. void _on_reconnect( unsigned p1, unsigned p2 );
  68. /**
  69. * @brief 失败回调函数。
  70. */
  71. void _on_fail();
  72. /**
  73. * @brief socket打开成功回调函数。
  74. */
  75. void _on_socket_opened();
  76. /**
  77. * @brief 收到服务器端CALL命令回调函数。
  78. * @param [in] std::string const& name 触发的名字\n
  79. * @param [in] sio::message::ptr const& data 收到的消息数据\n
  80. * @param [in] bool need_ack 是否需要应答\n
  81. * @param [in] sio::message::list &ack_resp 应答消息数据\n
  82. */
  83. void _OnCallMessage( std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp );
  84. /**
  85. * @brief 收到服务器端登录应答命令回调函数。
  86. * @param [in] std::string const& name 触发的名字\n
  87. * @param [in] sio::message::ptr const& data 收到的消息数据\n
  88. * @param [in] bool need_ack 是否需要应答\n
  89. * @param [in] sio::message::list &ack_resp 应答消息数据\n
  90. */
  91. void _OnLoginResponse( std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp );
  92. public:
  93. wsClient();
  94. ~wsClient();
  95. /**
  96. * @brief 初始化函数。
  97. * @param [in] int ID 当前客户端的唯一标识\n
  98. * @param [in] const std::string & uri 连接串\n
  99. * @param [in] const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList 消息处理函数\n
  100. */
  101. void init( int ID, const std::string & uri, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList );
  102. /**
  103. * @brief 获得连接字符串函数。
  104. * @return 获得的连接字符串\n
  105. */
  106. std::string get_uri();
  107. /**
  108. * @brief 连接函数。
  109. * @param [in] int time_out 超时(单位:秒),默认是3秒\n
  110. * @return 连接服务器是否成功\n
  111. * @retval 0 成功\n
  112. * @retval -1 失败\n
  113. */
  114. int connect( int time_out = 3 );
  115. /**
  116. * @brief ;登录函数。
  117. */
  118. void login();
  119. /**
  120. * @brief 发送数据函数。
  121. * @param [in] const std::string & Cmd 要发送的命令\n
  122. * @param [in] const std::string& Data 要发送的数据\n
  123. */
  124. void send( const std::string & Cmd, const std::string & Data );
  125. /**
  126. * @brief 获得ID函数。
  127. */
  128. int GetID();
  129. /**
  130. * @brief 获得是否已连接服务器端函数。
  131. */
  132. bool IsConnected();
  133. /**
  134. * @brief 获得Socket是否已打开函数。
  135. * @return Socket是否已打开\n
  136. */
  137. bool IsSktOpened();
  138. /**
  139. * @brief 获得是否已登录成功函数。
  140. * @return 是否已登录成功\n
  141. */
  142. bool IsLogined();
  143. /**
  144. * @brief 关闭函数。
  145. */
  146. void close();
  147. /**
  148. * @brief 获得最新错误函数。
  149. */
  150. std::string GetLastError();
  151. /**
  152. * @brief 获取删除ping的时间。
  153. */
  154. int GetPingTime() const ;
  155. };
  156. }