Browse Source

添加web调用日志

zzj 6 years ago
parent
commit
4d599d6fec
3 changed files with 86 additions and 2 deletions
  1. 71 0
      websocket/sio/sio_message.h
  2. 12 2
      websocket/wsClient.cpp
  3. 3 0
      websocket/wsClientMgr.cpp

+ 71 - 0
websocket/sio/sio_message.h

@@ -12,6 +12,7 @@
 #include <map>
 #include <cassert>
 #include <type_traits>
+#include <sstream>
 namespace sio
 {
     class message
@@ -58,6 +59,14 @@ namespace sio
             return 0;
         }
 
+        virtual std::string to_string() const
+        {
+            assert(false);
+            static std::string s_empty_string;
+            s_empty_string.clear();
+            return s_empty_string;
+        }
+
         virtual std::string const& get_string() const
         {
             assert(false);
@@ -147,6 +156,11 @@ namespace sio
         {
             return _v;
         }
+
+		std::string to_string()const
+		{
+			return _v?"true":"false";
+		}
     };
 
     class int_message : public message
@@ -173,6 +187,11 @@ namespace sio
         {
             return static_cast<double>(_v);
         }
+
+		std::string to_string()const
+		{
+			return std::to_string(_v);
+		}
     };
 
     class double_message : public message
@@ -193,6 +212,11 @@ namespace sio
         {
             return _v;
         }
+
+		std::string to_string()const
+		{
+			return std::to_string(_v);
+		}
     };
 
     class string_message : public message
@@ -222,6 +246,11 @@ namespace sio
         {
             return _v;
         }
+
+		std::string to_string()const
+		{
+			return "'"+_v+"'";
+		}
     };
 
     class binary_message : public message
@@ -335,6 +364,20 @@ namespace sio
         {
             return _v;
         }
+
+		std::string to_string()const
+		{
+			std::ostringstream ss;
+			ss<<"[";
+			for(size_t i=0;i<size();i++)
+			{
+				ss<<_v[i]->to_string()<<",";
+			}
+
+			ss<<"]";
+
+			return ss.str();
+		}
     };
 
     class object_message : public message
@@ -409,6 +452,20 @@ namespace sio
         {
             return _v;
         }
+
+		std::string to_string()const
+		{
+			std::ostringstream ss;
+			ss<<"{";
+			for(auto&it:_v)
+			{
+				ss<<it.first<<":"<<it.second->to_string()<<",";
+			}
+
+			ss<<"}";
+
+			return ss.str();
+		}
     };
 
     class message::list
@@ -561,6 +618,20 @@ namespace sio
             return arr;
         }
 
+		std::string to_string()const
+		{
+			std::ostringstream ss;
+			ss<<"[";
+			for(size_t i=0;i<size();i++)
+			{
+				ss<<m_vector[i]->to_string()<<",";
+			}
+
+			ss<<"]";
+
+			return ss.str();
+		}
+
     private:
         std::vector<message::ptr> m_vector;
     };

+ 12 - 2
websocket/wsClient.cpp

@@ -31,11 +31,21 @@ namespace YA
 		__Logined = false;
 	}
 
+	static void beatheart(int, std::string const&, sio::message::ptr const&, bool, sio::message::list & )
+	{
+	
+	}
+
 	void wsClient::init( int ID, const std::string & uri, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList )
 	{
 		__ID          = ID;
 		__uri         = uri;
 		__MsgFuncList = MsgFuncList;
+
+		//MsgFuncList.insert( std::make_pair( "beatheart", &web_connect::_beatheart_callback ) );
+
+		__MsgFuncList.insert(std::make_pair("beatheart",&beatheart));
+
 		__wsclient.set_reconnect_attempts( 0 );
 		using std::placeholders::_1;
 		using std::placeholders::_2;
@@ -193,7 +203,7 @@ namespace YA
 
 	void wsClient::_OnCallMessage( std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
 	{
-		log_info("web-message:%s",data->get_string().c_str());
+		log_info("web-message:%s",data->to_string().c_str());
 
 		if ( data->get_flag() == sio::message::flag_object ) 
 		{
@@ -218,7 +228,7 @@ namespace YA
 			}
 			else
 			{
-				log_error("web-message没有找到对应的处理函数:%s",data->get_string().c_str());
+				log_error("web-message没有找到对应的处理函数:%s",data->to_string().c_str());
 			}
 
 			__recv_ping_time = time(0);

+ 3 - 0
websocket/wsClientMgr.cpp

@@ -195,6 +195,8 @@ namespace YA
 		{
 			unsigned int t = w->GetPingTime();
 			// 间隔小于15秒 认为是正常
+
+//			log_info("now=%ud,ping=%ud,now-ping=%d",cur_time,t,cur_time-t);
 			if (w->IsConnected() && (t == 0 || cur_time - t < 15))
 			{
 				continue;
@@ -203,6 +205,7 @@ namespace YA
 			Disconnet(w);
 			std::this_thread::sleep_for(std::chrono::milliseconds(10));
 			Reconnect(w);
+			std::this_thread::sleep_for(std::chrono::seconds(3));
 		}
 	}
 }