123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #include "web_connect.h"
- #include<thread>
- #include<chrono>
- #include <tool_time.h>
- #include"log.h"
- #include"wsClientMgr.h"
- #include"wsTimerThread.h"
- #define BEATHEART_COUNT_NUM 1000
- web_connect::web_connect()
- {
- }
- //std::atomic<int> web_connect::_beatheart_count;
- void web_connect::init(const std::vector<std::string>&uri_list,int32_t st, std::map<std::string, sys::MSG_HANDLE_FUNC_TYPE>& MsgFuncList)
- {
- //MsgFuncList.insert( std::make_pair( "beatheart", &web_connect::_beatheart_callback ) );
- swsClientMgr.Build( uri_list, MsgFuncList );
- //init thread...
- sys::_THREAD_CONFIG_ Config;
- Config.SendInterval = st;
- swsTimerThrd.Init( Config );
- }
- void web_connect::start_beatheart_monitor()
- {
- auto th = std::thread(&web_connect::_beatheart_thread);
- th.detach();
- }
- void web_connect::close()
- {
- swsTimerThrd.Stop();
- swsClientMgr.close();
- }
- bool web_connect::connect()
- {
- swsTimerThrd.Start();
- //连接服务器
- if ( swsClientMgr.connect() != 0 )
- {
- log_error("web socket 连接失败....");
- std_error("websocket 连接失败....");
- return false;
- }
- //登录
- swsClientMgr.login();
- //swsTimerThrd.Start();
- return true;
- }
- //void web_connect::_beatheart_callback( int ID, std::string const& name,
- // sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
- //{
- // _beatheart_count=BEATHEART_COUNT_NUM;
- //}
- void web_connect::_beatheart_thread()
- {
- while (true)
- {
- std::this_thread::sleep_for(std::chrono::seconds(3));
- swsClientMgr.CheckClientConnect(tool_time::now_to_seconds());
- /*if(_beatheart_count==0)//一次都没收到web的心跳信号
- {
- log_info("没有收到web心跳信号");
- continue;
- }
- if(_beatheart_count==BEATHEART_COUNT_NUM)
- {
- _beatheart_count++;
- log_info("接收到web心跳信号");
- continue;
- }
- _beatheart_count=0;
- int count=0;
- while (true)
- {
- count++;
- if(count>=10000)
- {
- count=10000;
- }
- close();
- log_error("web连接异常中断, 开始重连web(第%d次)", count);
- std::this_thread::sleep_for(std::chrono::seconds(2));
- if(connect())
- {
- log_info("重连web ok");
- break;
- }
- }*/
- }
- }
|