#include "web_connect.h" #include #include #include #include"log.h" #include"wsClientMgr.h" #include"wsTimerThread.h" #define BEATHEART_COUNT_NUM 1000 web_connect::web_connect() { } //std::atomic web_connect::_beatheart_count; void web_connect::init(const std::vector&uri_list,int32_t st, std::map& 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; } }*/ } }