1
0

web_connect.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "web_connect.h"
  2. #include<thread>
  3. #include<chrono>
  4. #include <tool_time.h>
  5. #include"log.h"
  6. #include"wsClientMgr.h"
  7. #include"wsTimerThread.h"
  8. #define BEATHEART_COUNT_NUM 1000
  9. web_connect::web_connect()
  10. {
  11. }
  12. //std::atomic<int> web_connect::_beatheart_count;
  13. void web_connect::init(const std::vector<std::string>&uri_list,int32_t st, std::map<std::string, sys::MSG_HANDLE_FUNC_TYPE>& MsgFuncList)
  14. {
  15. //MsgFuncList.insert( std::make_pair( "beatheart", &web_connect::_beatheart_callback ) );
  16. swsClientMgr.Build( uri_list, MsgFuncList );
  17. //init thread...
  18. sys::_THREAD_CONFIG_ Config;
  19. Config.SendInterval = st;
  20. swsTimerThrd.Init( Config );
  21. }
  22. void web_connect::start_beatheart_monitor()
  23. {
  24. auto th = std::thread(&web_connect::_beatheart_thread);
  25. th.detach();
  26. }
  27. void web_connect::close()
  28. {
  29. swsTimerThrd.Stop();
  30. swsClientMgr.close();
  31. }
  32. bool web_connect::connect()
  33. {
  34. swsTimerThrd.Start();
  35. //连接服务器
  36. if ( swsClientMgr.connect() != 0 )
  37. {
  38. log_error("web socket 连接失败....");
  39. std_error("websocket 连接失败....");
  40. return false;
  41. }
  42. //登录
  43. swsClientMgr.login();
  44. //swsTimerThrd.Start();
  45. return true;
  46. }
  47. //void web_connect::_beatheart_callback( int ID, std::string const& name,
  48. // sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
  49. //{
  50. // _beatheart_count=BEATHEART_COUNT_NUM;
  51. //}
  52. void web_connect::_beatheart_thread()
  53. {
  54. while (true)
  55. {
  56. std::this_thread::sleep_for(std::chrono::seconds(3));
  57. swsClientMgr.CheckClientConnect(tool_time::now_to_seconds());
  58. /*if(_beatheart_count==0)//一次都没收到web的心跳信号
  59. {
  60. log_info("没有收到web心跳信号");
  61. continue;
  62. }
  63. if(_beatheart_count==BEATHEART_COUNT_NUM)
  64. {
  65. _beatheart_count++;
  66. log_info("接收到web心跳信号");
  67. continue;
  68. }
  69. _beatheart_count=0;
  70. int count=0;
  71. while (true)
  72. {
  73. count++;
  74. if(count>=10000)
  75. {
  76. count=10000;
  77. }
  78. close();
  79. log_error("web连接异常中断, 开始重连web(第%d次)", count);
  80. std::this_thread::sleep_for(std::chrono::seconds(2));
  81. if(connect())
  82. {
  83. log_info("重连web ok");
  84. break;
  85. }
  86. }*/
  87. }
  88. }