Преглед изворни кода

客户端的推送完成了

zhangyang пре 6 година
родитељ
комит
98017619dc

BIN
bin/Win32/Debug/zhipuzi_pos_windows/zhipuzi_pos_windows.exe


+ 2 - 3
lewaimai_dispatch/lewaimai_dispatch_windows.cpp

@@ -18,10 +18,9 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
 
 	//由这个对象来处理消息推送
 	boost::asio::io_context io_context;
-	CMessagePush push(io_context);
 
-	std::thread t(&CMessagePush::Start, &push);
-	t.detach();
+	CMessagePush push(io_context);
+	push.Start();
 
 	//开始展示窗口
     CPaintManagerUI::SetInstance(hInstance);

+ 24 - 30
lewaimai_dispatch/network/CMessagePush.cpp

@@ -10,9 +10,15 @@ CMessagePush::~CMessagePush()
 
 void CMessagePush::Start()
 {
+	std::thread t(&CMessagePush::Run, this);
+	t.detach();
+}
+
+void CMessagePush::Run()
+{
 	try
 	{
-		char host[] = "192.168.3.29";
+		char host[] = "127.0.0.1";
 		char port[] = "9001";
 
 		tcp::resolver resolver(m_io_context);
@@ -43,13 +49,13 @@ void CMessagePush::Start()
 			boost::bind(&CMessagePush::handle_read, this,
 				boost::asio::placeholders::error,
 				boost::asio::placeholders::bytes_transferred));
+
+		m_io_context.run();
 	}
 	catch (std::exception& e)
 	{
 		std::cerr << "Exception: " << e.what() << "\n";
 	}
-
-	CSystem::my_sleep(100);
 }
 
 void CMessagePush::PushMessage(std::string msg)
@@ -74,12 +80,22 @@ void CMessagePush::HandleMessage(std::string msg)
 		}
 
 		int type = document["type"].GetInt();
-		std::string username = document["username"].GetString();
-		std::string order_id = document["order_id"].GetString();
-		std::string order_no = document["order_no"].GetString();
 
-		if (type == 1)
+		if (type == 0)
+		{
+			std::string status = document["status"].GetString();
+
+			if (status == "ok")
+			{
+				//表示连接服务器成功了
+			}
+		}
+		else if (type == 1)
 		{
+			std::string username = document["username"].GetString();
+			std::string order_id = document["order_id"].GetString();
+			std::string order_no = document["order_no"].GetString();
+
 			CPosPrinter printer;
 			printer.PrintWaimaiOrder(order_id, order_no);
 		}
@@ -96,29 +112,7 @@ void CMessagePush::handle_read(const boost::system::error_code& error,
 	if (!error)
 	{
 		string s_reply = data_;
-
-		rapidjson::Document document;
-		document.Parse(s_reply.c_str());
-		if (!document.IsObject())
-		{
-			LOG_INFO("message 非法!");
-			return;
-		}
-
-		int type = document["type"].GetInt();
-		if (type == 0)
-		{
-			std::string status = document["status"].GetString();
-
-			if (status == "ok")
-			{
-				//表示连接服务器成功了
-			}
-		}
-		else
-		{
-			HandleMessage(s_reply);
-		}
+		HandleMessage(s_reply);
 
 		memset(data_, 0, max_length);
 		socket_.async_read_some(boost::asio::buffer(data_, max_length),

+ 2 - 0
lewaimai_dispatch/network/CMessagePush.h

@@ -27,6 +27,8 @@ public:
 
 	void Start();
 
+	void Run();
+
 	void StopWork()
 	{
 		m_is_work = false;

+ 0 - 2
lewaimai_pathplanning/lewaimai_pathplanning_windows.cpp

@@ -14,9 +14,7 @@ int main()
 	{
 		boost::asio::io_context io_context;
 
-		using namespace std;
 		CServer s(io_context, 9001);
-
 		io_context.run();
 	}
 	catch (std::exception& e)