1
0

socket.io-client-cpp_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // socket.io-client-cpp_test.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "sio_client.h"
  5. #include <functional>
  6. #include <iostream>
  7. #include <thread>
  8. #include <mutex>
  9. #include <condition_variable>
  10. #include <string>
  11. #include "constdef.h"
  12. #include <chrono>
  13. #include <stdio.h>
  14. #include <tchar.h>
  15. #include <rapidjson/writer.h>
  16. #include <rapidjson/stringbuffer.h>
  17. #include <rapidjson/prettywriter.h>
  18. #include "jsonCommon.h"
  19. #include <boost\locale.hpp>
  20. #include "wsClient.h"
  21. #include "wsClientMgr.h"
  22. #include "wsTimerThread.h"
  23. #include "singleton_test_class1.h"
  24. using namespace sio;
  25. using namespace std;
  26. std::mutex _lock;
  27. std::condition_variable_any _cond;
  28. bool connect_finish = false;
  29. bool socket_opened = false;
  30. class connection_listener
  31. {
  32. public:
  33. connection_listener()
  34. {
  35. }
  36. void on_connected()
  37. {
  38. std::cout << "sio connected" << std::endl;
  39. _lock.lock();
  40. _cond.notify_all();
  41. connect_finish = true;
  42. _lock.unlock();
  43. }
  44. void on_close( client::close_reason const& reason )
  45. {
  46. std::cout << "sio closed" << std::endl;
  47. exit( 0 );
  48. }
  49. void on_fail()
  50. {
  51. std::cout << "sio failed" << std::endl;
  52. exit( 0 );
  53. }
  54. void on_socket_opened()
  55. {
  56. std::cout << "on_socket_opened" << std::endl;
  57. _lock.lock();
  58. socket_opened = true;
  59. _cond.notify_all();
  60. _lock.unlock();
  61. }
  62. void OnLogin( std::string const& name, message::ptr const& data, bool isAck, message::list &ack_resp )
  63. {
  64. int res_code = ( int ) data->get_map()["code"]->get_int();
  65. switch ( res_code )
  66. {
  67. case -100: //登录失败
  68. {
  69. std::cout << "sio failed to login" << std::endl;
  70. break;
  71. }
  72. case 0:
  73. {
  74. if ( "" == data->get_map()["code"]->get_string() )
  75. { // 登录成功
  76. std::cout << "sio login ok" << std::endl;
  77. }
  78. else
  79. { // 退出登录
  80. std::cout << "sio failed to login" << std::endl;
  81. }
  82. break;
  83. }
  84. default:
  85. break;
  86. }
  87. }
  88. void OnCallMessage( string const& name, message::ptr const& data, bool isAck, message::list &ack_resp )
  89. {
  90. std::cout << "OnCallMessage, name=" << name.c_str() << std::endl;
  91. if ( data->get_flag() == message::flag_object )
  92. {
  93. string cmd = data->get_map()[JSON_ROOT_KEY_CMD]->get_string();
  94. char szLog[1024] = { 0 };
  95. sprintf( szLog, "received cmd from web: %s", cmd.c_str() );
  96. std::cout << szLog << std::endl;
  97. }
  98. }
  99. void OnReConnect( unsigned, unsigned )
  100. {
  101. std::cout << "sio OnReConnect" << std::endl;
  102. connect_finish = false;
  103. socket_opened = false;
  104. }
  105. };
  106. std::unique_ptr<sio::client> ws_client( new sio::client() );
  107. //sio::client ws_client;
  108. connection_listener l;
  109. socket::ptr current_socket;
  110. std::string BuildLogin( const YA::_JS_LOGIN_& Login )
  111. {
  112. rapidjson::StringBuffer sb;
  113. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  114. rapidjson::Document doc;
  115. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  116. rapidjson::Value root( rapidjson::kObjectType );
  117. rapidjson::Value child( rapidjson::kObjectType );
  118. rapidjson::Value key( rapidjson::kStringType );
  119. rapidjson::Value value( rapidjson::kStringType );
  120. key.SetString( "cmd", allocator );
  121. value.SetString( JSON_CMD_VALUE_LOGIN, allocator );
  122. root.AddMember( key, value, allocator );
  123. key.SetString( "user_name", allocator );
  124. value.SetString( Login.user_name.c_str(), allocator );
  125. child.AddMember( key, value, allocator );
  126. key.SetString( "user_pass", allocator );
  127. value.SetString( Login.user_password.c_str(), allocator );
  128. child.AddMember( key, value, allocator );
  129. key.SetString( "data", allocator );
  130. root.AddMember( key, child, allocator );
  131. root.Accept( writer );
  132. return sb.GetString();
  133. }
  134. //初始化
  135. void ws_init()
  136. {
  137. ws_client->set_reconnect_attempts( 0 );
  138. using std::placeholders::_1;
  139. using std::placeholders::_2;
  140. using std::placeholders::_3;
  141. using std::placeholders::_4;
  142. socket::ptr sock = ws_client->socket();
  143. //sock->on( JSON_CMD_VALUE_USER, sio::socket::event_listener_aux( std::bind( &connection_listener::OnLogin, &l, _1, _2, _3, _4 ) ) );
  144. //sock->on( "code", sio::socket::event_listener_aux( std::bind( &connection_listener::OnLogin, &l, _1, _2, _3, _4 ) ) );
  145. sock->on( JSON_CMD_VALUE_CALL, sio::socket::event_listener_aux( std::bind( &connection_listener::OnCallMessage, &l, _1, _2, _3, _4 ) ) );
  146. ws_client->set_open_listener( std::bind( &connection_listener::on_connected, &l ) );
  147. ws_client->set_close_listener( std::bind( &connection_listener::on_close, &l, std::placeholders::_1 ) );
  148. ws_client->set_reconnect_listener(std::bind(&connection_listener::OnReConnect, &l, std::placeholders::_1, std::placeholders::_2));
  149. ws_client->set_fail_listener( std::bind( &connection_listener::on_fail, &l ) );
  150. ws_client->set_socket_open_listener( std::bind( &connection_listener::on_socket_opened, &l ) );
  151. }
  152. //登录
  153. void ws_login()
  154. {
  155. std::cout << "ws_login()" << std::endl;
  156. YA::_JS_LOGIN_ Login;
  157. Login.user_name = JSON_VALUE_USERNAME;
  158. Login.user_password = JSON_VALUE_PASSWORD;
  159. std::string strLogin = BuildLogin( Login );
  160. strLogin += "\n";
  161. strLogin = boost::locale::conv::to_utf<char>( strLogin, "GBK" );
  162. current_socket->emit( JSON_CMD_VALUE_USER, strLogin, [&]( sio::message::list const& msglist )
  163. {
  164. message::ptr msg_ptr = msglist[0];
  165. int n = ( int ) msg_ptr->get_map()["code"]->get_int();
  166. if ( 0 == n )
  167. {
  168. std::cout << "Login ok, code=" << n << std::endl;
  169. }
  170. else
  171. {
  172. std::cout << "Login failed,code=" << n << std::endl;
  173. }
  174. } );
  175. }
  176. void simple_test()
  177. {
  178. //初始化
  179. ws_init();
  180. ws_client->connect( "ws://192.168.5.101:8086" );
  181. //ws_client->connect( "ws://192.168.8.175:8086" );
  182. //ws_client->connect( "ws://localhost:8086" );
  183. _lock.lock();
  184. if ( !connect_finish )
  185. {
  186. _cond.wait( _lock );
  187. }
  188. _lock.unlock();
  189. current_socket = ws_client->socket();
  190. _lock.lock();
  191. if ( !socket_opened )
  192. {
  193. _cond.wait( _lock );
  194. }
  195. _lock.unlock();
  196. std::this_thread::sleep_for( std::chrono::milliseconds( 500 ) );
  197. //登录
  198. ws_login();
  199. system( "PAUSE" );
  200. }
  201. //处理req_all_data命令的函数
  202. void On_req_all_data( int ID, std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
  203. {
  204. std::cout << "[Onreq_all_data] ";
  205. std::cout << "{" << ID << "} ";
  206. string strdata = data->get_map()["data"]->get_string().c_str();
  207. string version = data->get_map()["version"]->get_string().c_str();
  208. std::cout << "data=" << strdata.c_str() << ", version=" << version.c_str() << std::endl;
  209. }
  210. void On_req_all_person_on_car( int ID, std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
  211. {
  212. std::cout << "[On_req_all_person_on_car] ";
  213. std::cout << "{" << ID << "} ";
  214. std::vector<message::ptr> vdata = data->get_map()["data"]->get_vector();
  215. std::cout << vdata[0]->get_string() << ",";
  216. std::cout << vdata[1]->get_int() << std::endl;
  217. }
  218. void wsClient_class_test()
  219. {
  220. YA::wsClient client;
  221. std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
  222. MsgFuncList.insert( std::make_pair( "req_all_data", On_req_all_data ) );
  223. MsgFuncList.insert( std::make_pair( "req_all_person_on_car", On_req_all_person_on_car ) );
  224. //初始化
  225. client.init( 1, "ws://192.168.5.101:8086", MsgFuncList );
  226. //连接服务器端
  227. if ( client.connect() != 0 )
  228. {
  229. std::cout << "连接服务器失败" << std::endl;
  230. client.close();
  231. return;
  232. }
  233. //登录
  234. client.login();
  235. }
  236. void wsClientMgr_class_test()
  237. {
  238. //构造
  239. std::vector<std::string> uri_list;
  240. std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
  241. uri_list.push_back( "ws://192.168.5.101:8086" );
  242. //uri_list.push_back( "ws://192.168.5.102:8086" );
  243. MsgFuncList.insert( std::make_pair( "req_all_data", On_req_all_data ) );
  244. MsgFuncList.insert( std::make_pair( "req_all_person_on_car", On_req_all_person_on_car ) );
  245. swsClientMgr.Build( uri_list, MsgFuncList );
  246. //连接服务器
  247. if ( swsClientMgr.connect() != 0 )
  248. {
  249. std::cout << "连接服务器失败" << std::endl;
  250. return;
  251. }
  252. //登录
  253. swsClientMgr.login();
  254. std::shared_ptr<YA::wsClient> pClient;
  255. //通过URI获得某个websocket客户端
  256. pClient = swsClientMgr.GetClientByURI( "ws://192.168.5.101:8086" );
  257. if ( pClient != NULL )
  258. {
  259. std::cout << "ID=" << pClient->GetID() << ",URI=" << pClient->get_uri().c_str() << std::endl;
  260. }
  261. //通过下标获得某个websocket客户端
  262. pClient = swsClientMgr[1];
  263. if ( pClient != NULL )
  264. {
  265. std::cout << "ID=" << pClient->GetID() << ",URI=" << pClient->get_uri().c_str() << std::endl;
  266. }
  267. }
  268. void wsTimerThread_test()
  269. {
  270. YA::_THREAD_CONFIG_ Config;
  271. Config.SendInterval = 1;
  272. swsTimerThrd.Init( Config );
  273. swsTimerThrd.Start();
  274. YA::_CARD_POS_ cardpos;
  275. //602 人卡
  276. cardpos.ID = 602;
  277. cardpos.x = 3326.9895924094;
  278. cardpos.y = 100;
  279. cardpos.down_time = 1536161101000;
  280. cardpos.enter_area_time = 1536161561000;
  281. cardpos.rec_time = 1536163198000;
  282. cardpos.work_time = 2099000;
  283. cardpos.map_id = 5;
  284. cardpos.area_id = 32;
  285. cardpos.dept_id = 12;
  286. cardpos.stat = 0;
  287. cardpos.running_stat = 7;
  288. cardpos.biz_stat = 0;
  289. cardpos.speed = 6;
  290. cardpos.landmark_id = 27;
  291. cardpos.lm_direction = 2;
  292. cardpos.landmark_dis = 426;
  293. cardpos.level_id = 6;
  294. cardpos.is_on_duty = 0;
  295. cardpos.Type = 1;
  296. swsTimerThrd.upt_card_pos( cardpos );
  297. //603 人卡
  298. cardpos.ID = 603;
  299. cardpos.x = 3326.9895924094;
  300. cardpos.y = 100;
  301. cardpos.down_time = 1536161101000;
  302. cardpos.enter_area_time = 1536161561000;
  303. cardpos.rec_time = 1536163198000;
  304. cardpos.work_time = 2099000;
  305. cardpos.map_id = 5;
  306. cardpos.area_id = 22;
  307. cardpos.dept_id = 12;
  308. cardpos.stat = 0;
  309. cardpos.running_stat = 7;
  310. cardpos.biz_stat = 0;
  311. cardpos.speed = 6;
  312. cardpos.landmark_id = 27;
  313. cardpos.lm_direction = 2;
  314. cardpos.landmark_dis = 426;
  315. cardpos.level_id = 6;
  316. cardpos.is_on_duty = 0;
  317. cardpos.Type = 1;
  318. swsTimerThrd.upt_card_pos( cardpos );
  319. //197 人卡
  320. cardpos.ID = 197;
  321. cardpos.x = 4741.15;
  322. cardpos.y = -37.12;
  323. cardpos.down_time = 1534390846000;
  324. cardpos.enter_area_time = 1534403504000;
  325. cardpos.rec_time = 1534551770000;
  326. cardpos.work_time = 0;
  327. cardpos.map_id = 5;
  328. cardpos.area_id = 5;
  329. cardpos.dept_id = 2;
  330. cardpos.stat = 0;
  331. cardpos.running_stat = 0;
  332. cardpos.biz_stat = 0;
  333. cardpos.speed = 6;
  334. cardpos.landmark_id = 9;
  335. cardpos.lm_direction = 4;
  336. cardpos.landmark_dis = 10;
  337. cardpos.level_id = 0;
  338. cardpos.is_on_duty = 0;
  339. cardpos.Type = 2;
  340. swsTimerThrd.upt_card_pos( cardpos );
  341. //198 人卡
  342. cardpos.ID = 198;
  343. cardpos.x = 4741.15;
  344. cardpos.y = -37.12;
  345. cardpos.down_time = 1534390846000;
  346. cardpos.enter_area_time = 1534403504000;
  347. cardpos.rec_time = 1534551770000;
  348. cardpos.work_time = 0;
  349. cardpos.map_id = 5;
  350. cardpos.area_id = 6;
  351. cardpos.dept_id = 2;
  352. cardpos.stat = 0;
  353. cardpos.running_stat = 0;
  354. cardpos.biz_stat = 0;
  355. cardpos.speed = 6;
  356. cardpos.landmark_id = 9;
  357. cardpos.lm_direction = 4;
  358. cardpos.landmark_dis = 10;
  359. cardpos.level_id = 0;
  360. cardpos.is_on_duty = 0;
  361. cardpos.Type = 2;
  362. swsTimerThrd.upt_card_pos( cardpos );
  363. boost::this_thread::sleep( boost::posix_time::millisec( 5000 ) );
  364. swsTimerThrd.Stop();
  365. }
  366. int main()
  367. {
  368. //simple_test();
  369. //wsClient_class_test();
  370. //wsClientMgr_class_test();
  371. wsTimerThread_test();
  372. singleton_test_class1 st1;
  373. st1.DoSome();
  374. while ( true )
  375. {
  376. std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
  377. }
  378. return 0;
  379. }