wsClient.h 4.5 KB

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