#include "../pch/pch.h" #include "CMessagePush.h" #include "../tool/CPosPrinter.h" #include "../order/CWaimaiOrder.h" #include "mmsystem.h" CMessagePush::~CMessagePush() { } void CMessagePush::Start() { std::thread t(&CMessagePush::Run, this); t.detach(); } void CMessagePush::Run() { try { char host[] = "127.0.0.1"; char port[] = "9001"; tcp::resolver resolver(m_io_context); tcp::resolver::results_type endpoints = resolver.resolve(tcp::v4(), host, port); boost::asio::connect(socket_, endpoints); //发送身份信息 rapidjson::Document doc; doc.SetObject(); rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); //获取分配器 doc.AddMember("username", "zhangyang", allocator); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); doc.Accept(writer); //返回给接入层的消息 std::string m_login_msg = buffer.GetString(); //异步发送消息 PushMessage(m_login_msg); //同时启动一个异步接受消息 socket_.async_read_some(boost::asio::buffer(data_, max_length), 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"; } } void CMessagePush::PushMessage(std::string msg) { boost::asio::async_write(socket_, boost::asio::buffer(msg.c_str(), msg.length()), boost::bind(&CMessagePush::handle_write, this, boost::asio::placeholders::error)); } void CMessagePush::HandleMessage(std::string msg) { try { //收到服务器的消息,对服务器的消息进行处理 rapidjson::Document document; document.Parse(msg.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 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(); //新订单来了,首先判断是否要语音提醒 if (CSetting::GetParam("setting_is_new_waimai_voice") == "1") { wstring path = CSystem::GetProgramDir() + L"\\music\\new_wamai_order.wav"; PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC); } //判断是否要自动确认 if (CSetting::GetParam("setting_is_new_waimai_autoconfirm") == "1") { CWaimaiOrder newOrder; newOrder.ConfirmeOrder(order_id); } //判断是否右下角弹框提醒 if (CSetting::GetParam("setting_is_new_waimai_dialog") == "1") { } //判断是否自动打印 if (CSetting::GetParam("setting_is_new_waimai_printer") == "1") { CPosPrinter printer; printer.PrintWaimaiOrder(order_id, order_no); } CPosPrinter printer(2); printer.PrintWaimaiOrder(order_id, order_no); } } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } } void CMessagePush::handle_read(const boost::system::error_code& error, size_t bytes_transferred) { if (!error) { string s_reply = data_; HandleMessage(s_reply); memset(data_, 0, max_length); socket_.async_read_some(boost::asio::buffer(data_, max_length), boost::bind(&CMessagePush::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } else { //这里有时会出现,看看是什么错误,可能是断开了跟服务器的连接 } } void CMessagePush::handle_write(const boost::system::error_code& error) { if (!error) { LOG_INFO("send success!"); } else { //这里有时会出现,看看是什么错误,可能是断开了跟服务器的连接 } }